125 Top C# Interview Questions Dot Net Tricks

C# Interview Questions with Answers:-

1. Define what is C-Sharp (C#)?
C# is a type-safe, managed and object-oriented language, which is compiled by.Net framework for generating intermediate language (IL).

2. What are the features of C#?
Below are some of the features supported in C# –

Constructors and Destructors
Properties
Passing Parameters
Arrays
Main
XML Documentation and
Indexers

3. List some of the advantages of C#?
Below are the advantages of C# –

1. Easy to learn
2. Object-oriented
3. Component-oriented
4. Part of .NET framework

4. Define what is IDE’s provided by Microsoft for C# development?
Below are the IDE’s used for C# development –

Visual Studio Express (VCE)
Visual Studio (VS)
Visual Web Developer

5. What are the types of comments in C#?

Below are the types of comments in C# –

Single Line Comment
Multiline Comments
XML Comments

6. Explain sealed class in C#?

A sealed class is used to prevent the class from being inherited from other classes. So “sealed” modifier also can be used with methods to avoid the methods to override in the child classes.

7. Give an example of using a sealed class in C#?

Below is the sample code of sealed class in C# –

class X {}
sealed class Y : X {}

Sealed methods –

class A
{
protected virtual void First() { }
protected virtual void Second() { }
}
class B : A
{
sealed protected override void First() {}
protected override void Second() { }
}
If any class inherits from class “B” then method – “First” will not be overridable as this method is sealed in class B.

8. List out the differences between Array and ArrayList in C#?

Array stores the values or elements of the same data type but ArrayList stores values of different data types. Arrays will use the fixed length but ArrayList does not use fixed length like an array.

9. Why use “using” in C#?

“Using” statement calls – “dispose of” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read-only and cannot be reassignable or modifiable.

10. Explain namespaces in C#?

Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespaces.

11. Why use the keyword “const” in C#? Give an example.

“Const” keyword is used for making an entity constant. We can’t reassign the value to constant.

Eg: const string _name = “Test”;

12. Define what is the difference between “constant” and “read-only” variables in C#?

“Const” keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables. “read-only” variable value can be changed during runtime and value to read-only variables can be assigned in the constructor or at the time of declaration.

13. Explain the “static” keyword in C#?

“Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.

14. Define what is the difference between “dispose of” and “finalize” variables in C#?

Dispose of – This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files, etc.
Finalize – This method is called internally unlike Dispose method which is called explicitly. It is called by the garbage collector and can’t be called from the code.

15. How the exception handling is done in C#?

In C# there is a “try… catch” block to handle the error.

16. Can we execute multiple catch blocks in C#?

No. Once an exception has occurred it executes specific exception catch block and the control comes out.

17. Why use “finally” block in C#?
“Finally” block will be executed irrespective of exception. So while executing the code in try block when an exception occurs, control is returned to catch block and at last “finally” block will be executed. So the closing connection to database / releasing the file handlers can be kept in “finally” block.

18. Define what is the difference between “finalize” and “finally” methods in C#?

Finalize – This method is used for garbage collection. So before destroying an object, this method is called as part of the cleanup activity.
Finally – This method is used for executing the code irrespective of exception occurred or not.

19. Define what is the difference between “throw ex” and “throw” methods in C#?

“throw ex” will replace the stack trace of the exception with stack trace info of rethrowing point. “throw” will preserve the original stack trace info.

20. Can we have only “try” block without “catch” block in C#?

Yes, we can have only try block without catch block.

21. List out two different types of errors in C#?

Below are the types of errors in C# –

1. Compile Time Error
2. Run Time Error

22. Do we get an error while executing the “finally” block in C#?

Yes. We may get an error in finally block.

23. Mention the assembly name where System namespace lies in C#?

Assembly Name – mscorlib.dll

24. Define what are the differences between static, public and void in C#?

1. Static classes/methods/variables are accessible throughout the application without creating an instance. The compiler will store the method address as an entry point.
2. Public methods or variables are accessible throughout the application.
3. The void is used for the methods to indicate it will not return any value.

25. Define what is the difference between “out” and “ref” parameters in C#?

“out” parameter can be passed to a method and it need not be initialized whereas “ref” parameter has to be initialized before it is used.

26. Define Jagged Arrays in C#?

If the elements of an array is an array then it’s called a jagged array. The elements can be of different sizes and dimensions.

27. Can we use “this” inside a static method in C#?

No. We can’t use “this” in a static method.

28. Define what are value types in C#?

Below is the list of value types in C# –

decimal
int
byte
enum
double
long
float

29. Define what are reference types in C#?

Below is the list of reference types in C# –

1. class
2. string
3. interface
4. object

30. Can we override the private virtual method in C#?

No. We can’t override private virtual methods as it is not accessible
outside the class.

31. Define what is serialization?

Serialization is the process of converting an object into a stream of bytes. De-serialization is the opposite process of creating an object from a stream of bytes. Serialization / Deserialization is mostly used to transport objects.

32. Define what are the difference between Structure and Class?

1. Structures are value type and Classes are a reference type
2. Structures can not have contractors or destructors.
3. Classes can have both contractors and destructors.
4. Structures do not support Inheritance, while Classes support Inheritance.

33. Define what is the difference between Class And Interface?

Class is a logical representation of an object. It is a collection of data and related sub procedures with a definition.
The interface is also a class containing methods which are not having any definitions. Class does not support multiple inheritances. But interface can support.

34. Define what is Delegates?

Delegates are a type-safe, object-oriented implementation of function pointers and are used in many situations where a component needs to call back to the component that is using it.

35. Define what is Authentication and Authorization?

Authentication is the process of identifying users. Authentication is identifying/validating the user against the credentials (username and password). Authorization performs after authentication. Authorization is the process of granting access to those users based on identity. Authorization allowing access to the specific resource to the user.

36. Define what is a base class?

A class declaration may specify a base class by following the class name with a colon and the name of the base class. omitting a base class specification is the same as deriving from type object.

37. Can “this” be used within a static method?

No ‘This’ cannot be used in a static method. As only static variables/methods can be used in a static method.

38. Define what is the difference between constants, readonly and, static?

1. Constants: The value can’t be changed.
2. Read-only: The value will be initialized only once from the constructor of the class.
3. Static: Value can be initialized once.

39. Define what are the different types of statements supported in C#?

C# supports several different kinds of statements are

Block statements
Declaration statements
Expression statements
Selection statements
Iteration statements
Jump statements
Try catch statements
Checked and unchecked
Lock statement

40. Define what is an interface class?

It is an abstract class with public abstract methods all of which must be implemented in the inherited classes.

41. Define what are value types and reference types?

Value types are stored in the Stack.
Examples: bool, byte, chat, decimal, double, enum, float, int, long, byte, short, strut, uint, ulong, short.

Reference types are stored in the Heap.
Examples: class, delegate, interface, object, string.

42. Define what is the difference between string keyword and System. String class?

String keyword is an alias for System. String class. Therefore, the System. String and string keyword are the same, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings.

43. Define what are the two data types available in C#?

Value type
Reference type

44. Define what are the different types of Caching?

There are three types of Caching:

1. Output Caching: stores the responses from an asp.net page.
2. Fragment Caching: Only caches/stores the portion of the page (User Control)
3. Data Caching: is Programmatic way to Cache objects for performance.

45. Define what is the difference between Custom Control and User Control?

Custom Controls have compiled the code (DLLs), easier to use, difficult to create, and can be placed in toolbox. Drag and Drop controls. Attributes can be set visually at design time. Can be used by Multiple Applications (If Shared DLLs), Even if Private can copy to the bin directory of web application add reference and use. Normally designed to provide common functionality independent of consuming Application. User Controls are similar to those of ASP include files, easy to create, can not be placed in the toolbox and dragged – dropped from it. A User Control is shared among the single application files.

46. Define what is methods?

A method is a member that implements a computation or action that can be performed by an object or class. Static methods are accessed through the class. Instance methods are accessed through instances of the class.

47. Define what is fields?

A field is a variable that is associated with a class or with an instance of a class.

48. Define what is events?

An event is a member that enables a class or object to provide notifications. An event is declared like a field except that the declaration includes an event keyword and the type must be a delegate type.

49. Define what is literals and their types?

Literals are value constants assigned to variables in a program. C# supports several types of literals are

Integer Literals
Real literals
Boolean Literals
Single character literals
String Literals
Backslash character literals

50. Define what is the difference between value type and reference type?

1. Value types are stored on the stack and when a value of a variable is assigned to another variable.
2. Reference types are stored on the heap, and when an assignment between two reference variables occurs.

51. Define what are the features of c#?

1. C# is a simple and powerful programming language for writing enterprise edition applications.
2. This is a hybrid of C++ and VB. It retains many C++ features in the area statements, expressions, and operators and incorporated the productivity of VB.
3. C# helps the developers to easily build web services that can be used across the Internet through any language, on any platform.
4. C# helps the developers accomplishing with fewer lines of code that will lead to fewer errors in the code.
5. C# introduces considerable improvement and innovations in areas such as type safety, versioning. events and garbage collections.

52. Define what are the types of errors?

Syntax error
Logic error
Runtime error

53. Define what is the difference between break and continue statement?

The break statement is used to terminate the current enclosing loop or conditional statements in which it appears. We have already used the break statement to come out of switch statements. The continue statement is used to alter the sequence of execution. Instead of coming out of the loop as the break statement did, the continue statement stops the current iteration and simply returns control back to the top of the loop.

54. Define namespace?

The namespace is known as containers which will be used to organize the hierarchical set of.Net classes.

55. Define what is a code group?

A code group is a set of assemblies that share a security context.

56. Define what are sealed classes in C#?

The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class.

57. Define what is the difference between static and instance methods?

A method declared with a static modifier is a static method. A static method does not operate on a specific instance and can only access static members. A method declared without a static modifier is an instance method. An instance method operates on a specific instance and can access both static and instance members. The instance on which an instance method was invoked can be explicitly accessed as this. It is an error to refer to this in a static method.

58. Define what are the different types of variables in C#?

Different types of variables used in C# are :

static variables
instance variable
value parameters
reference parameters
array elements
output parameters
local variables

59. Define what is meant by method overloading?

Method overloading permits multiple methods in the same class to have the same name as long as they have unique signatures. When compiling an invocation of an overloaded method, the compiler uses overload resolution to determine the specific method to invoke.

60. Define what is the parameters?

Parameters are used to pass values or variable references to methods. The parameters of a method get their actual values from the arguments that are specified when the method is invoked. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays.

61. Is C# is object oriented?

Yes, C# is an OO language in the tradition of Java and C++.

62. Define what is the difference between Array and ArrayList?

An array is a collection of the same type. The size of the array is fixed in its declaration. A linked list is similar to an array but it doesn’t have a limited size.

63. Define what are the special operators in C#?

C# supports the following special operators.

is (relational operator)
as (relational operator)
type (tape operator)
sizeof (size operator)
new (object creator)
.dot (member access operator)
checked (overflow checking)
unchecked (prevention of overflow checking)

64. Define what is meant by operators in c#?

An operator is a member that defines the meaning of applying a particular expression operator to instances of a class. Three kinds of operators can be defined: unary operators, binary operators, and conversion operators. All operators must be declared as public and static.

65. Define what is a parameterized type?

A parameterized type is a type that is parameterized over another value or type.

66. Define what are the features of abstract class?

1. An abstract class cannot be instantiated, and it is an error to use the new operator on an abstract class.
2. An abstract class is permitted (but not required) to contain abstract methods and accessors.
3. An abstract class cannot be scaled.

67. Define what is the use of the abstract keyword?

The modifier abstract is a keyword used with a class, to indicate that this class cannot itself have direct instances or objects, and it is intended to be only a ‘base’ class to other classes.

68. Define what is the use of a goto statement?

The goto statement is also included in the C# language. This goto can be used to jump from inside a loop to outside. But jumping from outside to inside a loop is not allowed.

69. Define what is the difference between console and window application?

A console application, which is designed to run at the command line with no user interface. A Windows application, which is designed to run on a user’s desktop and has a user interface.

70. Define what is the use of the return statement?

The return statement is associated with procedures (methods or functions). On executing the return statement, the system passes the control from the called procedure to the calling procedure. This return statement is used for two purposes: to return immediately to the caller of the currently executed code
to return some value to the caller of the currently executed code.

71. Define what is the difference between Array and LinkedList?

The array is a simple sequence of numbers which are not concerned about each other’s positions. they are independent of each other’s positions. adding, removing or modifying any array element is very easy. Compared to arrays, a linked list is a complicated sequence of numbers.

72. Does C# have a throws clause?

No, unlike Java, C# does not require the developer to specify the exceptions that a method can throw.

73. Does C# support a variable number of arguments?

Yes, using the params keyword. The arguments are specified as a list of arguments of a specific type.

74. Can you override private virtual methods?

No, private methods are not accessible outside the class.

75. Define what is a multicast delegate?

Each delegate object holds a reference to a single method. However, it is possible for a delegate object to hold references and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.

76. Which is an exclusive feature of C#?

XML documentation.

77. Is using of exceptions in C# recommended?

Yes, exceptions are the recommended error handling mechanism in .NET Framework.

78. Define what does a break statement do in switch statements?

The break statement terminates the loop in which it exists. It also changes the flow of the execution of a program. In switch statements, the break statement is used at the end of a case statement. The break statement is mandatory in C# and it avoids the fall through of one case statement to another.

79. Is C# object-oriented?

Yes, C# is an OO language in the tradition of Java and C++.

80. Define what is smart navigation?

The cursor position is maintained when the page gets refreshed due to the server side validation and the page gets refreshed.

81. Define what is the difference between CONST and READONLY?

Both are meant for constant values. A const field can only be initialized at the declaration of the field. A read-only field can be initialized either at the declaration or.

82. Does C# have a throws clause?

No, unlike Java, C# does not require (or even allow) the developer to specify the exceptions that a method can throw.

83. Define what are the different ways a method can be overloaded?

Different parameter data types, different number of parameters, different order of parameters.

84. Do events have return type?

No, events do not have a return type.

85. Define what is an event?

An event is an action performed based on another method of the program. An event is a delegate type dass member that is used by an object or a class to provide a notification to other objects that an event has occurred. An event can be declared with the help of the event keyword.

86. Define what is an identifier?

Identifiers are nothing but names given to various entities uniquely identified in a program.

87. Define what are the different types of literals in C#?

Boolean literals: True and False are literals of the Boolean type that map to the true and false state, respectively.

Integer literals: Used to write values of types Int, uint, long, and ulong.

Real literals: Used to write values of types float, double, and dermal.

Character literals: Represents a single character and usually consists
of a character in quotes, such as ‘a’.

String literals: C# supports two types of string literals, a regular string literal and verbatim string literals. A regular string literal consists of zero or more characters enclosed in double quotes, such as “116110”. A verbatim string literal consists of an @ character followed by a double–quote character, such as ©” hello”.

The Null literal: Represents the null–type.

88. Define what is meant by data encapsulation?

Data encapsulation also referred to as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions called methods.

89. Can you override private virtual methods?

No. Private methods are not accessible outside the class.

90. Define what is the main difference between a sub procedure and a function?

Sub procedures do not return a value, while functions do.

91. How does C# differ from C++?

C# does not support the #include statement. It uses only using statement. In C#, the class definition does not use a semicolon at the end. C# does not support multiple code inheritance. Casting in C# is much safer than in c++. In C# switch can also be used on string values. Command line parameters array behave differently in C# as compared to C++.

92. Define what is nested class?

Nested classes are classes within classes. A nested class is any class whose declaration occurs within the body of another class or interface.

93. Can you have parameters for static constructors?

No, static constructors cannot have parameters.

94. Is String is Value Type or Reference Type in C#?

A string is an object(Reference Type).

95. Does C# provide copy constructor?

No, C# does not provide copy constructor.

96. Can a class or a struct have multiple constructors?

Yes, a class or a struct can have multiple constructors. Constructors in C# can be overloaded.

97. Can you create an instance of an interface?

No, you cannot create an instance of an interface.

98. Can an Interface contain fields?

No, an Interface cannot contain fields.

99. Can a class have a static constructor?

Yes, a class can have a static constructor. Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are referenced. Static constructors are called before instance constructors. An example is shown below.

100. Define what is the main use of delegates in C#?

Delegates are mainly used to define call back methods.

101. Define what is the difference between Shadowing and Overriding?

Overriding redefines only the implementation while shadowing redefines the whole element. In overriding derived classes can refer the parent class element by using “ME” keyword, but in shadowing you can access it by “MY BASE”.

102. Can events have access to modifiers?

Yes, you can have access to modifiers in events. You can have events with the protected keyword, which will be accessible only to inherited classes. You can have private events only for objects in that class.

103. Why is the virtual keyword used in code?

The Virtual keyword is used in code to define methods and the properties that can be overridden in derived classes.

104. Define what are constructors and destructors?

Constructors and destructors are special methods. Constructors and destructors are special methods for every class.
Each class has its own constructor and destructor and are called automatically when the instance of a class is created or destroyed. The constructor initializes all class members whenever you access the class and the destructor destroys them when the objects are not required anymore.

105. How can we suppress a finalize method?

GC.SuppressFinalize()

106. Does C# support a variable number of arguments?

Yes, using the params keyword. The arguments are specified as a list of arguments of a specific type, e.g., int. For ultimate flexibility, the type can object. The standard example of a method which uses this approach is System.console.writeLine().

107. Which method will you call to start a thread?

Start

108. Define what is Generic?

1. Generic help us to create flexible strong type collection.
2. Generic basically separate the logic from the datatype in order to maintain better reusability, better maintainability, etc.

109. Define what are the different types of polymorphism?

There are two types of polymorphism. They are:

Compile time Polymorphism
Run time Polymorphism

110. Define what is the difference between compile-time polymorphism and run-time polymorphism?

Compile time Polymorphism Compile time Polymorphism also known as method overloading. Method overloading means having two or more methods with the same name but with different signatures. Run time Polymorphism
Run time Polymorphism also known as method overriding. Method overriding means having two or more methods with the same name, same signature but with a different implementation.

111. Which namespace enables multithreaded programming in XML?

System.Threading

112. Can we declare a block as static in c#?

No, because c# does not support static block, but it supports static method.

113. Can we declare a method as sealed?

In C# a method can’t be declared as sealed. However, when we override a method in a derived class, we can declare the overridden method as sealed. By declaring it as sealed, we can avoid further overriding of this method.

114. Define what Command is used to implement properties in C#?

get & set access modifiers are used to implement properties in c#.

115. Define what is a static member?

The member defined as static which can be invoked directly from the class level, rather than from its instance.

116. Define what is the syntax to inherit from a class in C#?

When a class is derived from another class, then the members of the base class become the members of the derived class. The access modifier used while accessing members of the base class specifies the access status of the base class members inside the derived class. The syntax to inherit a class from another class In C# is as follows:

class MyNewClass : MyBaseClass

117. Define what is a basic difference between the while loop and do-while loop in C#?

The while loop tests its condition at the beginning, which means that the enclosed set of statements run for zero or more number of times if the condition evaluates to true. The do while loop iterates a set of statements at least once and then checks the condition at the end.

118. Define what is the main difference between a sub procedure and a function?

Sub procedures do not return a value, while functions do.

119. Define what are sealed classes in c#?

The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class.

120. Define what is the difference between a class and an Interface?

1. Abstract classes can have implementations for some of its members, but the interface can’t have implementation for any of its members.
2. Interfaces cannot have fields whereas an abstract class can have fields.
3. An interface can inherit from another interface only and cannot inherit from an abstract class, whereas an abstract class can inherit from another abstract class or another interface.
4. A class can inherit from multiple interfaces at the same time, whereas a class cannot inherit from multiple classes at the same time.
5. Abstract class members can have access modifiers whereas interface members cannot have access modifiers.

121. Define what is the difference between an abstract method & virtual method?

An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), whereas the virtual method has an implementation and leaves an option to override it in the deriving class. Thus Virtual method has an implementation & provides the derived class with the
option of overriding it. The abstract method does not provide implementation & forces the derived class to override the method.

122. Define what is the Static Method?

It is possible to declare a method as Static provided that they don’t attempt to access any instance data or other instance methods.

123. Define what is a New modifier?

The new modifier hides a member of the base class. C# supports only hide by signature.

124. Define what are the advantages of getting and set properties in C#?

1. The get property accessor is used to return the property value.
2. The set property accessor is used to assign a new value.

125. Define what are the difference between const and readonly?

A const cannot be static, while readonly can be static. A const need to be declared and initialized at declaration only,
while a readonly can be initialized at the declaration or by the code in the constructor. A const’s value is evaluated at design time, while a read-only value is evaluated at runtime.