C#.Net Interview Questions and Answer
1. What is C#.Net?
C# is a object oriented language for building Microsoft .NET software applications. Developers can build almost every kind of software using C# including Windows UI apps, console apps, backend services, cloud APIs, Web services, controls and libraries, server less applications, Web applications, native iOS and Android apps, AI and machine learning software, and blockchain applications.
C# syntaxes are like C++. .NET and the C# library is similar to Java. C# supports modern object-oriented programming language features including Abstraction, Encapsulation, Polymorphism, and Inheritance. C# is a strongly typed language and most types are inherited by the Object class.
C# supports concepts of classes and objects. Classes have members such as fields, properties, events, and methods. Here is a detailed article on C# and OOP.
2.What is an Object in C# and syntax to create it?
An object is an instance of a class. A class can have as many instances as needed. For example, Honda Civic is an instance of Car. In real programming, Honda Civic is an object. Honda Civic is an instance of the class Car. The Model, Type, Color, and Size properties of Honda Civic are Civic, Honda, Red, 4 respectively. BMW 330, Toyota Carolla, Ford 350, Honda CR4, Honda Accord, and Honda Pilot are some more examples of objects of Car.
Car HondaCivic = new Car();
3. What is Managed or Unmanaged Code?
Managed Code
“Managed code is the code that is developed using the .NET framework and its supported programming languages such as C# or VB.NET. Managed code is directly executed by the Common Language Runtime (CLR or Runtime) and its lifecycle including object creation, memory allocation, and object disposal is managed by the Runtime. Any language that is written in .NET Framework is managed code".
Ex: C#.net, VB.Net and F# etc.
Unmanaged Code The code that is developed outside of the .NET framework is known as unmanaged code.
Ex: File Descriptor, Network connect etc.
Applications that do not run under the control of the CLR are said to be unmanaged. Languages such as C or C++ or Visual Basic are unmanaged.
The object creation, execution, and disposal of unmanaged code is directly managed by the programmers. If programmers write bad code, it may lead to memory leaks and unwanted resource allocations.” The .NET Framework provides a mechanism for unmanaged code to be used in managed code and vice versa. The process is done with the help of wrapper classes.
4. What is Boxing and Unboxing in C#?
Boxing and Unboxing both are used for type conversions.
The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion. Here is an example of boxing in C#.
- // Boxing
- int anum = 123;
- Object obj = anum;
- ======================================================================
- // Unboxing
- Object obj2 = 123;
- int anum2 = (int)obj;
5. What is the difference between a struct and a class in C#?
Class and struct are both user-defined data types, but have some major differences:
- The struct is a value type in C# and it inherits from System.Value Type.
- Struct is usually used for smaller amounts of data.
- Struct can’t be inherited from other types.
- A structure can't be abstract.
- No need to create an object with a new keyword.
- Do not have permission to create any default constructor.
Class
- The class is a reference type in C# and it inherits from the System.Object Type.
- Classes are usually used for large amounts of data.
- Classes can be inherited from other classes.
- A class can be an abstract type.
- We can create a default constructor.
6. What is the difference between Interface and Abstract Class in C#?
Here are some of the common differences between an interface and an abstract class in C#.
- A class can implement any number of interfaces but a subclass can at most use only one abstract class.
- An abstract class can have non-abstract methods (concrete methods) while in case of interface, all the methods have to be abstract.
- An abstract class can declare or use any variables while an interface is not allowed to do so.
- In an abstract class, all data members or functions are private by default while in an interface all are public, we can’t change them manually.
- In an abstract class, we need to use abstract keywords to declare abstract methods, while in an interface we don’t need to use that.
- An abstract class can’t be used for multiple inheritance while the interface can be used as multiple inheritance.
- An abstract class use constructor while in an interface we don’t have any type of constructor.
- Interface is helpful for injecting dependency injection and creating unit the test cases for interface methods.

0 Comments
If you have any doubts then let me know