Write A Program To Print Numbers Between 1 To 100 Except 10 Using C#
Write A Program To Print Numbers Between 1 To 100 Except 10 Using C#
Write A Program To Print Numbers Between 1 To 100 Except 10 Using C#
namespace print_number
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("print numbers between 1 to 100 except 10");
int i;
for (i = 0; i <= 100; i++)
{
if (i == 10)
{
continue;
}
Console.WriteLine(+i);
}
}
}
}
2. Describe MVC with diagram? Write a program in
Asp.Net MVC for Student Registration that contains field
for Full Name, Email and Address.
MVC stands for Model, View, and Controller. MVC separates an application into three
components - Model, View, and Controller.
1. Model: - Model represents the shape of the data. A class in C# is used to describe a
model. Model objects store data retrieved from the database.
2. Controller: - The controller handles the user request. Typically, the user uses the view
and raises an HTTP request, which will be handled by the controller. The controller
processes the request and returns the appropriate view as a response.
3. View:- View in MVC is a user interface. View display model data to the user and also
enables them to modify them. View in ASP.NET MVC is HTML, CSS, and some special
syntax (Razor syntax) that makes it easy to communicate with the model and the
controller.
Diagram of MVC
Model
namespace WebApplication6.Models
{
public class Student
{
public int Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
}
ApplicationDbContext
namespace WebApplication7.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
}
}
Startup
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
Appsettings.json
{
"ConnectionStrings": {
"DefaultConnection":
"Server=.;Database=Student;Trusted_Connection=True;MultipleActiveResultSets=true"
},
Controller
namespace WebApplication7.Controllers
{
public class StudentsController : Controller
{
private readonly ApplicationDbContext _context;
public StudentsController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,FullName,Email,Address")] Student
student)
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}
}
Result:
3. What is .Net framework? Describe in brief. Also describe
compilation and execution of .Net Framework with diagram.
.Net Framework is a software development platform developed by Microsoft for building and
running Windows applications. The .Net framework consists of developer tools, programming
languages, and libraries to build desktop and web applications. It is also used to build websites,
web services, and games. It is developed by Microsoft in 2002 and It uses C# and Vb.net.
The .Net framework was meant to create applications, which would run on the
Windows Platform. The first version of the .Net framework was released in the year 2002. The
version was called .Net framework 1.0. The Microsoft .Net framework has come a long way
since then, and the current version is .Net Framework 4.7.2. The Microsoft .Net framework can
be used to create both – Form-based and Web-based applications. Web Server can also be
developed using the .Net framework.
Compiler convert source code into the MSIL (Microsoft intermediate Language) code in
the form of an assembly through CLS, CTS. IL is the language that CLR can understand. On
execution, this IL is converted into binary code by CLR’s just in time compiler (JIT) and these
assemblies or DLL are loaded in the memory.
4. What is access modifier? Describe with examples.
Access modifiers in C# are used to specify the scope of accessibility of a member of a class
or type of the class itself. For example, a public class is accessible to everyone without any
restrictions, while an internal class may be accessible to the assembly only.
In C# there are 6 different types of Access Modifiers.
Modifier Description
public There are no restrictions on accessing public members.
Access is limited to within the class definition. This is the default access
private
modifier type if none is formally specified
Access is limited to within the class definition and any class that inherits
protected
from the class
Access is limited exclusively to classes defined within the current project
internal
assembly
Access is limited to the current assembly and types derived from the
protected
containing class. All members in current project and all members in
internal
derived class can access the variables.
private Access is limited to the containing class or types derived from the
protected containing class within the current assembly.
Inheritance is the process of acquiring all the properties of one class into another class.
There are two classes referred to as base class and derived class. The base class is also
known as a parent class, and the properties or methods of this class we want to inherit
to another class.
The derived class is known as the child class that is used to inherit the properties and
methods of the base class or parent class. It helps in reusing the same code again, and
no need to define the same properties again and again.
Types of inheritance
1. Single Level Inheritance
In Single inheritance, there is only one base class and one derived class. It means the
child class will inherit the properties of the parent class and use them.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyAplication{
class Demo {
static void Main(string[] args) {
// Father class
Father f = new Father();
f.Display();
// Son class
Son s = new Son();
s.Display();
s.DisplayOne();
Console.ReadKey();
}
class Father {
public void Display()
{
Console.WriteLine("Display");
}
}
class Son : Father {
public void DisplayOne()
{
Console.WriteLine("DisplayOne");
}
}
}
}
Output:
2. Multilevel Inheritance
In this type of inheritance, there is only one base class, and multiple derived class are available.
If a class is created by using another derived class is known as multilevel inheritance.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo{
class Son : Father {
public void DisplayTwo() {
Console.WriteLine("Son.. ");
}
static void Main(string[] args) {
Son s = new Son();
s.Display();
s.DisplayOne();
s.DisplayTwo();
Console.Read();
}
}
class Grandfather {
public void Display() {
Console.WriteLine("Grandfather...");
}
}
class Father : Grandfather {
public void DisplayOne() {
Console.WriteLine("Father...");
}
}
}
Output:
6. What are advantages of partial class? Create two partial classes in C#. One
partial class should contain the employee’s details such as id, firstname,
lastname and salary. And the second partial class should contain a method to
display all the information of the employee.
Model
Namespace WebApplication6.Models
{
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal Salary { get; set; }
}
}
Controller
namespace WebApplication7.Controllers
{
public class PartialController : Controller
{
public IActionResult Index()
{
return PartialView();
}
}
}
PartialController1
namespace WebApplication7.Controllers
{
public class PartialListController : Controller
{
public IActionResult Index()
{
return PartialView();
}
}
}
PartialController2
namespace WebApplication7.Controllers
{
public class EmployeeController : Controller
{
private readonly ApplicationDbContext _context;
// GET: Students
public async Task<IActionResult> Index()
{
return View(await _context. Employee.ToListAsync());
}
Output:
Example:
// Data source
string[] names = {"Bill", "Steve", "James", "Mohan" };
// LINQ Query
var myLinqQuery = from name in names
where name.Contains('a')
select name;
// Query execution
foreach(var name in myLinqQuery)
Console.Write(name + " ");
using System;
[HttpGet]
Public class EmployeeController : controller
{
Public IActionResult Index()
{
Return view();
}
}
[HttpPost]
Public class EmployeeController : controller
{
Public IActionResult Create(Model)
{
Var Model = new Model();
Return view();
}
}
[HttpPut]
Public class EmployeeController : controller
{
Public IActionResult Update(int id, Employee)
{
_context.update(Employee);
Return view();
}
}
[HttpDelete]
Public class EmployeeController:Controller
{
Public IActionResult Delete(id)
{
var employee = _context. Employee.Find(id);
_context. Employee.Remove(employee);
_context.savechange();
}
11. What is MVC patterns? Describe in brief with examples
MVC stands for Model, View, and Controller. MVC separates an application
into three components - Model, View, and Controller.
1. Model: - Model represents the shape of the data. A class in C# is used
to describe a model. Model objects store data retrieved from the
database.
2. Controller: - The controller handles the user request. Typically, the
user uses the view and raises an HTTP request, which will be handled
by the controller. The controller processes the request and returns the
appropriate view as a response.
3. View:- View in MVC is a user interface. View display model data to the
user and also enables them to modify them. View in ASP.NET MVC is
HTML, CSS, and some special syntax (Razor syntax) that makes it
easy to communicate with the model and the controller.
Diagram Of MVC
Example
Model
[Required]
public string Name { get; set; }
}
Controller
@model Project.Models.People
@Html.InputFieldFor(model => model.Name)
12. Write SQL query to select, insert, update and delete of given fields.
ID, FirstName, LastName, Address, Salary, DOB
1.Create
CREATE TABLE Student (
Id int,
firstname varchar(120),
lastname varchar(200),
address varchar(120),
dob datetime,);
2.Insert
INSERT INTO Student (firstname, lastname, address, dob)
VALUES (aashish, pudasaini, kathmandu, 2056);
3.Select
Select * from Student
Select * from Student where firstname = ‘aashish’
4.Delete
Delete * from student
Delete * from student where lastname = ‘pudasaini’
5.Update
UPDATE Student
SET firstname = Ashok
Where id = 1
13 Write switch case program .If user types a/e/i/o/u then output should be
“vowel” else “not vowel”
namespace ConsoleApp3{
class Program {
static void Main(string[] args){
var ok = Console.ReadLine();
switch(ok) {
case "a":
case "e":
case "i":
case "o":
case "u":
Console.WriteLine("Vowel");
break;
default:
Console.WriteLine("not vowel");
break;
}
}
}
}
14 What are differences between for loop and foreach
loop in C# with examples?