Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

1.

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.

Example of Access Modifier


Using System;  
namespace AccessModifiers  
{  
    class Program  
    {  
        class AccessMod  
        {  
            public int num1;  
        }  
        static void Main(string[] args)  
        {  
            AccessMod ob1 = new AccessMod();  
            //Direct access to public members  
            ob1.num1 = 100;  
            Console.WriteLine("Number one value in main {0}", ob1.num1);  
            Console.ReadLine();  
        }   } }
Output:
5. Describe types of inheritance with examples.

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.

Inheritance is one of the powerful concepts or fundamental attributes of object-


oriented programming language. It is widely used in all the OOPs based programming
language. Its main purpose is to use the same code 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;

public Employee Controller(ApplicationDbContext context)


{
_context = context;
}

// GET: Students
public async Task<IActionResult> Index()
{
return View(await _context. Employee.ToListAsync());
}

Output:

7. What is a LINQ query? Explain LINQ syntax and give an


example.
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data
from different sources and formats. It is integrated in C# or VB, thereby eliminating the
mismatch between programming languages and databases, as well as providing a single
querying interface for different types of data sources.
For example, SQL is a Structured Query Language used to save and retrieve data from a
database. In the same way, LINQ is a structured query syntax built in C# and VB.NET to retrieve
data from different types of data sources such as collections, ADO.Net DataSet, XML Docs, web
service and MS SQL Server and other databases.

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 + " ");

8. Write a program to concate given letters using C# Asian,


College, of, Management

using System;

public class GFG {


static public void Main()
{
string strA = " Asian";
string StrB = “Collage”;
string StrC = “of”;
string strD = "Management";
string str;
Console.WriteLine("String A is: {0}", strA);
Console.WriteLine("String B is: {0}", strB);
Console.WriteLine("String B is: {0}", strC);
Console.WriteLine("String B is: {0}", strD);
str = String.Concat(strA, strB, strC, strD);
Console.WriteLine("Concatenated string is: {0}", str);
}
}

9. Describe in brief about .NET core. What are differences between


.NET core and .NET?
.NET Core is a new version of .NET Framework, which is a free, open-source, general-purpose
development platform maintained by Microsoft. It is a cross-platform framework that runs on
Windows, macOS, and Linux operating systems.
.NET Core Framework can be used to build different types of applications such as mobile,
desktop, web, cloud, IoT, machine learning, microservices, game, etc.
.NET Core is written from scratch to make it modular, lightweight, fast, and cross-platform
Framework. It includes the core features that are required to run a basic .NET Core app. Other
features are provided as NuGet packages, which you can add it in your application as needed. In
this way, the .NET Core application speed up the performance, reduce the memory footprint
and becomes easy to maintain.
.NET Core Characteristics
Open-source Framework: .NET Core is an open-source framework maintained by
Microsoft and available on GitHub under MIT and Apache 2 licenses. It is a .NET Foundation
project.
You can view, download, or contribute to the source code using the following GitHub
repositories:
Language compiler platform Roslyn: https://github.com/dotnet/roslyn
.NET Core runtime: https://github.com/dotnet/runtime
.NET Core SDK repository. https://github.com/dotnet/sdk
ASP.NET Core repository. https://github.com/dotnet/aspnetcore
Cross-platform: .NET Core runs on Windows, macOS, and Linux operating systems. There
are different runtime for each operating system that executes the code and generates the same
output.
Consistent across Architectures: Execute the code with the same behavior in
different instruction set architectures, including x64, x86, and ARM.
Wide-range of Applications: Various types of applications can be developed and run
on .NET Core platform such as mobile, desktop, web, cloud, IoT, machine learning,
microservices, game, etc.
Supports Multiple Languages: You can use C#, F#, and Visual Basic programming
languages to develop .NET Core applications. You can use your favorite IDE, including Visual
Studio 2017/2019, Visual Studio Code, Sublime Text, Vim, etc.
Modular Architecture: .NET Core supports modular architecture approach using NuGet
packages. There are different NuGet packages for various features that can be added to the
.NET Core project as needed. Even the .NET Core library is provided as a NuGet package. The
NuGet package for the default .NET Core application model is Microsoft.NETCore.App.
This way, it reduces the memory footprint, speeds up the performance, and easy to maintain.
CLI Tools: .NET Core includes CLI tools (Command-line interface) for development and
continuous-integration.
Flexible Deployment: .NET Core application can be deployed user-wide or system-wide
or with Docker Containers.

10.  What is Asp.NET Web Api? Describe GET, POST, PUT and


DELETE method with examples?
ASP.NET Web API is a framework for building HTTP services that can be accessed from any
client including browsers and mobile devices. It is an ideal platform for building RESTful
applications on the .NET Framework.

[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

public class Person


{
public int id { get; set; }

[Required]
public string Name { get; set; }
}

Controller

public class PeopleController : Controller


{
Public IActionResult()
{
Return view();
}
View

@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?

You might also like