0% found this document useful (0 votes)
21 views

Interview Questions

c # Interview Questions

Uploaded by

alimomin661
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Interview Questions

c # Interview Questions

Uploaded by

alimomin661
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Interview Questions .

Net
1. Explain the difference between .NET and C# ?
Ans: C# is a programming language developed by
Microsoft. .NET is a software development framework by
Microsoft. C# is one of the languages used to develop
applications within the .NET framework.
2. .NET Framework(MVC) vs. .NET Core
Ans: .NET Framework is a Windows-only framework for
building Windows applications and Can only be used on
Windows based Machine (Version 4.8).
.NET Core is a cross-platform framework. It is to build to
cross platform Web Applications.We Can use It on any OS like
windows, MacOs , Linux. The Current Version Is 7 and 8 is yet
to come.
3. What is IL (Intermediate Language) Code?
Ans: IL (Intermediate Language) also known as
MSIL(Microsoft Intermediate Language) code is a code
generated by .NET Language specific compilers. It is compiled
to machine code by the JIT compiler at runtime.
4. What is the use of JIT?
JIT (Just-in-Time Compiler) compiler translates IL code into
machine code at runtime, ensuring platform independence
and enabling performance optimizations.
5. Is it possible to view IL code?
 Yes, you can view IL code using tools like ILDASM or by
enabling IL code generation in Visual Studio.
Interview Questions .Net
6: What is the benefit of compiling into IL code?
Ans : Compiling into IL code allows platform independence
and performance optimizations during JIT compilation.
7: Does .NET support multiple programming languages?
Ans : Yes, .NET supports multiple languages like C#, VB.NET,
and F# that compile to a common IL code.
8: What is CLR (Common Language Runtime)?
The Common Language Runtime (CLR) is a component of the
Microsoft .NET Framework. It Converts the IL Code into
Native (Machine Code) .It also Invokes Just-In-Time (JIT)
compiler, which compiles the IL code into machine code on
the fly as the program runs.
Interview Questions .Net
9: What is managed and unmanaged code?
A code which is executed by the CLR is known as Managed
code.
A code which is directly executed by the operating system is
known as Unmanaged code.
10: Explain the importance of Garbage Collector?
GC (Garbage collector) collects all objects that are no longer
used by the application and then makes them free
from memory.
11: Can garbage collector claim unmanaged objects?
No, the Garbage Collector can't manage unmanaged objects
As it is Not Handled by CLR.
12: : Difference between Stack vs. Heap?
ANS : Stack and Heap are 2 Portion on the RAM. Stack is used
to store the Value Types. Heap is used to store the Reference
type Data.
13: What are Value types & Reference types?
Value types store their data directly on the Stack, while
reference types store Memory address on the stack and the
actual data in the heap.
16: Explain boxing and unboxing?
Boxing converts a value type to an Object type, while
unboxing extracts the value type from object.
Interview Questions .Net
17: Explain casting, implicit casting, and explicit casting?
Casting is the conversion of one data type to another. In
Implicit casting a smaller Data Type is Converted in to a bigger
data type eg:- int is converted in bool type
In Explicit Casting a bigger Data Type is Converted in to a
smaller data type eg:- bool is converted in int type
18: What can happen during explicit casting?
During explicit casting, data loss can occur.
19: Differentiate between Array and ArrayList?
Arrays have a fixed lenght, store elements of the same type,
and are more efficient. ArrayList has flexible lenght and it is
not type safe.
ArrayList Syntax -
NameSpace - System.Collections;
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
Array Syntax –
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars[0]);
// Outputs Volvo
Interview Questions .Net
22: What are generic collections?
Generic collections in .NET allow you to create type-safe
collections. Generic List are Type Safe and also have flexible
length.
23: How do we handle exceptions in C# (try/catch)?
In C#, you use a try-catch block to handle exceptions. Code
that may throw an exception is placed within the try block,
and the exception-handling logic is placed within the catch
block.
24: What is the need for "finally"?
The finally block is used to ensure that a piece of code
executes regardless of whether an exception is thrown or not.
25:What is Delegates?
A delegate is an object which refers to a method or you can
say it is a reference type variable that can hold a reference to
the methods.
[modifier] delegate [return_type] [delegate_name]
([parameter_list]);
You can create a delegate by defining a delegate type and
assigning a method to it.
Syntax example:
public delegate int MathOperation(int a, int b);
MathOperation add = (a, b) => a + b;
Interview Questions .Net
26:Why do we need OOP?
Object-Oriented Programming (OOP) provides a way to model
real-world entities, organize code, and promote code
reusability. It enhances modularity, maintainability, and
extensibility of software.
27: What are the important pillars of OOP?
The four important pillars of Object-Oriented Programming
(OOP) are:
1-Encapsulation: Bundling data and methods into a single
unit (class).
2-Abstraction: Hiding complex details and showing only the
necessary features.(Method)
3-Inheritance: Inheriting a class with another class to access
properties and Methods with : Symbol.
Types of Inheritance in C#:- Single level – Class B : Class A
Multi-Level - – Class B : Class A , Class C : Class B
Hierarchical inheritance - Class B : Class A , Class C : Class A,
Class D : Class A
4-Polymorphism: The polymorphism means taking shapes.
Example-Method and Constructor Overloading. Same name
but different parameters
There are two types of polymorphism in C#:
1)Static / Compile Time Polymorphism – Method Overloading
2) Dynamic / Runtime Polymorphism – Method Overriding
Interview Questions .Net
28: Explain the "virtual" keyword?
In C#, the "virtual" keyword is used to declare a method in a
base class that can be overridden in derived classes.
29: Overloading vs Overriding?
Overloading involves multiple methods with the same name
but different parameters within the same class. Overriding
involves providing a new implementation for a method in a
derived class that is originally declared as virtual or abstract
in the base class.
30: Why do we need Abstract classes?
An abstract class in C# is a class that cannot be instantiated
and is made to be used as a base class for other classes. An
abstract class can contain abstract methods, which are
methods without an implementation, and must be
overridden in a derived class. An abstract class can also
contain non-abstract methods, which have an
implementation, and can be overridden in a derived class if
needed. These methods can be used as common
functionality for all derived classes. In summary, the purpose
of an abstract class in C# is to provide a common interface
and implementation for derived classes.
31: Can we create an instance of Abstract classes?
No, you cannot create an instance (Object) of an abstract
class.
 Can we create a Constructor of Abstract classes?
YES It will bring the properties of Abstract class in Living State
Interview Questions .Net
Difference between Abstract Class and Interface in C#

Abstract Class Interface

An abstract class can consist of An interface can have only abstract


abstract and non-abstract methods. methods.

Abstract classes do not support An interface allows multiple


multiple inheritance. inheritance.

Abstract classes can have static Interfaces can not have static members
members. .

They can have methods, constants, They can have only indexers, methods,
fields, etc. events, and properties.

You need to use You need to use the interface keyword


the abstract keyword to declare an to declare an interface.
abstract class.

Abstract classes have a constructor. Interfaces don’t have a constructor.

They are faster than interfaces in They are slower than abstract classes
terms of performance. in terms of performance.

It consists of both declaration and It only consists of a declaration without


definition. definition.

32 : Method Shadowing
Interview Questions .Net
Method shadowing in C# is a way to create a new method in
a derived class that has the same name as a method in the
base class, without intending to override the base class
method. The new method "shadows" or hides the base class
method. When you call the method on an instance of the
derived class, the new method in the derived class is
executed, not the one in the base class. It's like having two
methods with the same name, but they are separate and
don't have any connection to each other.
33: Shadowing vs Overriding?
Shadowing and overriding are different mechanisms.
Overriding is used to provide a new implementation for a
method in a derived class, while shadowing is used to create
a new method that has the same name as a base class
method but does not override it.
34 :When do we need Shadowing?
Shadowing allows you to override the behavior of a parent
class member in the derived class. This can be
important . when you want to change the behavior of a base
class member to match the requirements of the derived
class.
35:Explain Sealed Classes?
Sealed classes are used to restrict the users from inheriting
the class. A class can be sealed by using the sealed keyword.
The keyword tells the compiler that the class is sealed, and
therefore, cannot be extended. No class can be derived from
a sealed class.
Interview Questions .Net
36:What are nested classes?
A nested class in C# is like a class inside another class. It's a
way to group related classes together. Think of it as putting a
smaller box inside a bigger box.The special thing about nested
classes is that they can see and use the stuff (variables,
methods) in the outer (or containing) class. So, it's like the
smaller box can access things in the bigger box.They can also
access private members of the outer class.
Can nested classes access outer class variables?
Yes, nested classes can access private members (variables,
methods) of the outer class. This is known as an inner class's
ability to capture the outer class's context.
Can we have public and protected access modifiers in
nested classes?
Yes, you can use public and protected access modifiers for
nested classes.
37:What is SOLID?-The goal of SOLID principles is to create
software that is easier to maintain, extend, and adapt to
changing requirements while reducing the risk of introducing
bugs during modification.
 S: Single Responsibility Principle (SRP)
 O: Open-closed Principle (OCP)
 L: Liskov substitution Principle (LSP)
 I: Interface Segregation Principle (ISP)
 D: Dependency Inversion Principle (DIP)
Interview Questions .Net

1. Single Responsibility Principle (SRP): A class should


have just one job or responsibility. It's like a good worker
who does one task well without getting distracted by
other things.
2. Open/Closed Principle (OCP): Code should be open for
extension but closed for modification. Imagine you have
a toy. You can add new parts to make it better, but you
don't have to break it to make changes.
3. Liskov Substitution Principle (LSP): Subclasses should be
able to replace their base class without causing
problems. Think of it like replacing your regular bicycle
wheel with a more advanced one without changing the
entire bike.
4. Interface Segregation Principle (ISP): Don't make classes
implement interfaces they don't need. It's like having a
menu with only the items you want to eat instead of a
long list of everything.
5. Dependency Inversion Principle (DIP): High-level
modules should not depend on low-level modules. Both
should depend on abstractions (interfaces). It's like a
remote control that works with many different TV
brands without being hardcoded to one.
Interview Questions .Net
38)What is Dependency Injection ?
D.I is a technique to make our project Decoupled and Module
Independent In simple words the Objects which are meant to
be used in whole project are Initialize at a place and then
used in whole project. We don’t have to use ‘new’ keyword
again and again to make a object everywhere.
In .NET Core DI has built-in support but in MVC (4.8) we have
to install a package named ‘Autofac’ to use the functionality
of Dependency injection .We have to register our Controller
in Application_Start and then we have to register our type
and write the Class name of whose Object we need. Using
the SingleInstence(); Method will prevent the duplicating of
objects.
Note – Use Documentation of AutoFac MVC for Codes
How MVC works ?
MVC (Model-View-Controller) is a design pattern commonly
used in software development, and it's also used in the .NET
Framework for building web applications
Model:The Model represents the data and business logic of
your application. In ASP.NET MVC, models are often created
as C# classes that define the structure of your data.
View: The View is responsible for presenting data to the user
and handling user interface components.Views in ASP.NET
MVC are typically implemented as Razor views, which are a
mixture of HTML and C# code.
Interview Questions .Net
Controller: The Controller acts as an intermediary between
the Model and the View. It receives user input from the
browser, processes it, interacts with the Model to retrieve or
update data, and then selects the appropriate View to render
as a response.
The typical flow of an ASP.NET MVC application works like
this:
1. A user makes a request by entering a URL in their
browser.
2. The ASP.NET MVC framework's routing mechanism maps
the URL to a specific controller and action method.
3. The controller's action method is invoked, and it may
interact with the Model to retrieve data or update data.
4. The action method selects a View and passes data from
the Model to it.
5. The View renders the HTML response, which is then sent
back to the user's browser.

39)What is LINQ ? its importance in .NET


LINQ (Language Integrated Query) Converts the C# code into
SQL Query to Interract with Database. It enables developers
to query and manipulate data using a SQL-like syntax directly
within C# or other .NET languages. LINQ provides a
consistent, type-safe, and efficient way to work with various
data sources, including collections, arrays, databases, XML,
and more.
Interview Questions .Net
40)How to Bind data ?
Strongly-Typed View
41)String vs Stringbuilder
string: Immutable: A string in C# is immutable, which
means once you create a string object, you cannot modify it.
Any operation that appears to modify a string actually creates
a new string.
Memory Overhead: Because of immutability, creating,
concatenating, or modifying strings results in the creation of
new string objects, which can lead to increased memory
consumption.
Use When: Use string when you need to represent fixed,
unchanging text data, and you don't expect to perform many
modifications to the text.
StringBuilder:
 Mutable: Unlike strings, StringBuilder is mutable. It allows
you to modify the content of a string without creating
new objects. This is more efficient when you need to
perform a lot of string concatenation or manipulation.
 Efficient for Modifications: StringBuilder is designed for
efficient string modifications, such as appending,
inserting, or removing characters from a string.
 Use When: Use StringBuilder when you need to
frequently modify a string, especially in scenarios like
building dynamic SQL queries, constructing large strings,
or any operation involving many string manipulations.
Interview Questions .Net
42)ViewData:
ViewData is similar to ViewBag in that it allows you to pass
data from a controller to a view. However, ViewData is a
dictionary-like container, and you need to cast data when
retrieving it in the view. It uses ViewData Dictionary to store
all the names and description of all objects.
Controller
public ActionResult Index()
{
ViewData["Message"] = "Hello, ViewData!";
return View();
}
View
<p>@((string)ViewData["Message"])</p>

43)ViewBag
ViewBag is a dynamic property bag in ASP.NET MVC that
allows you to pass data from a controller action to a view. It
uses the dynamic keyword to store data, which means you
can assign any type of data to it. It also uses ViewData
Dictionary to store all the names and description of all
objects.
Interview Questions .Net
Controller:
public ActionResult Index()
{
ViewBag.Message = "Hello, ViewBag!";
return View();
}
view:
<p>@ViewBag.Message</p>
44) TempData is used to store data that needs to be retained
for the next HTTP request. This is particularly useful for
scenarios like redirecting to another action and passing data
along with the redirection.It is implemented as a dictionary
and is often used to store data between actions in the same
controller or when using the RedirectToAction method.

public ActionResult FirstAction()


{
TempData["Message"] = "This message will survive the
redirect.";
return RedirectToAction("SecondAction");
}
Interview Questions .Net
ANOTHER CONTROLLER
public ActionResult SecondAction()
{
string message = TempData["Message"] as string;
// Use the message
return View();
}

45)DDL and DML Commands


DDL – Data Definition Language
List of DDL commands:
CREATE DROP ALTER TRUNCATE COMMENT RENAME.
DML – Data Manipulation Language
INSERT UPDATE DELETE LOCK

47 )Virtual keyword: In C#, the virtual keyword is used to


indicate that a method or property in a base class can be
overridden by derived classes.

48) Readonly Access modifier:

49) Readonly Access modifier:

The readonly modifier in C# is used with fields to make them


read-only after their initial value is assigned. This means the
field can only be assigned a value at the time of declaration
or within the constructor of the class.
Interview Questions .Net
46)What is a Stored Procedure?

A stored procedure is a prepared SQL code that you can save,


so the code can be reused over and over again. So if you have
an SQL query that you write over and over again, save it as a
stored procedure, and then just call it to execute it.

Stored Procedure Syntax


CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;
Execute the stored procedure :
EXEC SelectAllCustomers;

50) Function in SQL:

In SQL, a function is a reusable SQL code block that can


accept input parameters, perform specific actions, and return
a result.

SQL Function Syntax


CREATE FUNCTION AddNumbers(@num1 INT ,@num2 INT)
RETURNS INT
AS
BEGIN
RETURN @num1 + @num2 ;
END;
Interview Questions .Net
Calling a SQL Function
SELECT dbo.AddNumbers(10 , 10) AS Sum ;

Difference in Stored Procedure and Function

Return Value:

 Stored Procedure: A stored procedure doesn't have to


return a value. It can be used for performing actions or
modifying data in the database. It may or may not
return a result.

 Function: A function is designed to return a single, scalar


value. It must return a value when called

Data Modification:

 Stored Procedure: Stored procedures can modify data in


the database, including inserts, updates, and deletes.

 Function: Functions are generally used for calculations


and data retrieval; they don't directly modify data.

Input Parameters:

 Stored Procedure: Stored procedures can accept input


parameters, which allow for dynamic behavior based on
the provided inputs.

 Function: Functions also accept input parameters, which


are used in calculations or to filter results.
Interview Questions .Net
51) ADO.NET:
ADO.NET was an ORM Tool used before Entity Framework
E.F also uses ADO,NET Internally.

52) Entity Framework


It is an Object-Relational Mapping (ORM) tool. Entity
Framework simplifies the process of interacting with a
database, whether you're building a web application with
ASP.NET MVC or a web API with ASP.NET Web API. It
promotes a more object-oriented and developer-friendly
approach to database access, reducing the need for writing
raw SQL queries.
Data Modeling: EF allows you to define the structure of your
database using C# classes. Each property in an entity class
corresponds to a column in the database.
Data Access: EF handles the communication with the
database. It generates SQL queries for you based on your C#
code, and it can execute CRUD (Create, Read, Update,
Delete) operations on the database.
LINQ Integration: EF allows you to use LINQ (Language
Integrated Query) to write queries in C# that are translated
into SQL for the database.
Interview Questions .Net
53) What is Foreign Key ?
So If we want to add relation between two tables in our
RDBMS or want to use the entity of another table in the main
table to do so we have to make a property in the main table
of the other to access the data from other table.
Imagine there are 2 tables in our project One Customer table
and one Membershiptype table. So if we want to access the
meembershiptype of a customer we have to make FOREIGN
KEY of the Membershiptype table in the Customer table.
It is Done by making a property in Customer table with the
name of membershiptype like this
public MembershipType MembershipType { get; set; }
and then we have to make an Id prop.. to access the
membership.. table like this
[ForeignKey("MembershipType")]
public int MembershipTypeId { get; set; }
for making it foreign key we have to give attribute to it of
Foreign Key like in the above Code.

53) SQL Joins


Interview Questions .Net
A JOIN clause is used to combine rows from two or more
tables, based on a related column between them.
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in
both tables
Syntax :
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
LEFT (OUTER) JOIN: Returns all records from the left table,
and the matched records from the right table
Syntax :
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT (OUTER) JOIN: Returns all records from the right table,
and the matched records from the left table
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Interview Questions .Net
FULL (OUTER) JOIN: Returns all records when there is a
match in either left or right table
Syntax :
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Interview Questions .Net

You might also like