Most Asked Questions in C# Interview

By HH Team | Views: 443

1) What is C#?
C# is a simple, modern, general-purpose programming language that is compiled by the .Net framework to generate Microsoft Intermediate Language.

2) What is an object in C#?
An object is an instance of a class through which we access the methods of that class. "New" keyword is used to create an object. A class that creates an object in memory will contain information about the methods, variables, and behavior of that class.

3) What is Boxing and Unboxing in C#?
Boxing and Unboxing are both used for type converting, but have some differences:

Boxing
Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR converting a value type to Object Type, it wraps the value inside a System. Object and stores it on the heap area in the application domain.

Example:
public void Function1()
{
int i  = 111;
object  o  =  i;     //implict boxing
Console.WriteLine(o);
}

Unboxing
Unboxing is also a process that is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing has to be explicit by code. 

Example:
public void Function1()
{
int i  = 111;
object  i = (int)o;  //explict Unboxing
Console.WriteLine(i);
}

The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object. 

4) Define Constructors in C#?
A constructor is a member function in a class that has the same name as its class. The constructor is automatically invoked whenever an object class is created. It constructs the values of data members while initializing the class.
There are five types of constructors:
Static constructor
Private constructor
Copy constructor
Default constructor
Parameterized constructor


5) What is the difference between method overriding and method overloading?
In Method overriding, we change the method definition in the derived class that changes the method behavior. While In Method overloading is a mechanism to create multiple methods with the same name and unique signature in the same class. When you go for compilation, the compiler uses overload resolution to determine the specific method to be invoked.

6)  What is the difference between "is" and "as" operators in c#?

"is" operator is used to checking the compatibility of an object with a given type, and it returns the result as Boolean.

"as" operator is used for casting an object to a type or a class.

7)  What is an array? Also, define an array list?

An Array is a set of related instances either value or reference types.

There are three types of array supported by C#:
Single Dimensional Array: It contains a single row. It is also known as a vector array.
Multi-Dimensional Array: It is rectangular and contains rows and columns.
Jagged Array: It also contains rows and columns but it has an irregular shape.

ArrayList is a dynamic array. You can add and remove the elements from an ArrayList at runtime. In the ArrayList, elements are not automatically sorted.

8) How a method can be overloaded?
Methods can be overloaded using different data types for a parameter, different order of parameters, and a different number of parameters.

9)  Can multiple catch blocks be executed?
No, Multiple catch blocks of a similar type can't be executed. Once the proper catch code executed, the control is transferred to the final block, and then the code that follows the final block gets executed.

10) What is the difference between public, static, and void?
Public declared variables or methods are accessible anywhere in the application. 
Static declared variables or methods are globally accessible without creating an instance of the class. Static members are by default not globally accessible it depends upon the type of access modified used. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. And,
The void is a type of modifier that states that the method or variable does not return any value.

11) What is the use of the 'using' statement in C#?
The 'using' block is used to obtain a resource and process it and then automatically dispose of it when the execution of the block completed.

12) Can we use the "this" command within a static method?
We can't use ''this'' in a static method because we can only use static variables/methods in a static method.

13) What is the difference between constants and read-only?
Constant variables are declared and initialized at compile time. The value can't be changed afterward.
Read-only is used only when we want to assign the value at run time.

14) Can a private virtual method can be overridden?
No, because they are not accessible outside the class.

15) What is enum in C#? 
Enumeration is a user-defined data type in C language. It is used to assign names to the integral constants which make a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.

16) What is the lock statement in C#?
Lock statement is used to ensure that one thread doesn't enter a critical section of code while another thread is in the critical section. If another thread attempts to enter a locked code it will wait, block, until the object is released.

17)  Which are the access modifiers available in C#?
Following are the access modifiers generally used for accessibility:

Public: If you define an attribute or method as public, it can be accessed from any code of the project.
Private: A private defined attribute or method can be accessed by any code within the containing class only.
Protected: If you define the method or attribute as protected it can be accessed by any method in the inherited classes and any method within the same class.
Internal: If you define an attribute or a method as internal, it is restricted to classes within the current position assembly.
Protected Internal: If you define an attribute or method as protected internal, access is restricted to classes within the current project assembly or types derived from the containing class.

18) What is the difference between early binding and late binding in C#?
Early binding and late binding are the concepts of polymorphism.

There are two types of polymorphism in C#.
Compile Time Polymorphism: It is also known as early binding.
Run Time Polymorphism: It is also known as late binding or method overriding or dynamic polymorphism.

19) What is Garbage Collection?
Garbage Collection is a process of releasing memory automatically occupied by objects which are no longer accessible.

20) What's the difference between an interface and an abstract class?
Interfaces have all the methods having only declaration but no definition. In an abstract class, we can have some concrete methods. In an interface class, all the methods are public. An abstract class may have private methods.

21)  What are circular references?
A circular reference is a situation in which two or more resources are interdependent on each other causes the lock condition and make the resources unusable.

22) What are generics in C#.NET?
Generics are used to make reusable code classes to decrease the code redundancy, increase type safety, and performance. Using generics, we can create collection classes. To create a generic collection, System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace. Generics promotes the usage of parameterized types.

23) What is an object pool in .NET?
An object pool is a container having objects ready to be used. It tracks the object that is currently in use, the total number of objects in the pool. This reduces the overhead of creating and re-creating objects.

24) What's a multicast delegate?
A delegate having multiple handlers assigned to it is called a multicast delegate. Each handler is assigned to a method.

25) What is the difference between the "throw" and "throw ex" in .NET?
The "Throw" statement preserves the original error stack whereas the "throw ex" has the stack trace from their throw point. It is always advised to use "throw" because it provides more accurate error information.

26) Is C# code is managed or unmanaged code?
C# is managed code because Common language runtime can compile C# code to Intermediate language.

 

Also Read: 

Cyberattack on the US govt may have started earlier than initially thought.

Thank you for your feedback!