C# interview questions and answers

16) How do you get the size of value type?

using SizeOf operator.
Example: Console.WriteLine("The size of long is {0}.", sizeof(long));
output: The size of long is 8.

17) What are different types of the parameter in c#?

Following are different types of parameter in c#
1. Value parameter(in)
2. Reference parameter (ref)
3. Output parameter(out)
4. Parameter Array(param)

18) 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 where as “ref” parameter has to be initialized before it is used.

19) 18. List out the differences between Array and ArrayList in C#?

1. Arrays are strongly typed collection of same datatype and these arrays are fixed length that cannot be changed during runtime.
2. Arrays belong to System.Array namespace.

1. Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type.
2. Arraylist belongs to System.Collection namespaces


20) Why we use keyword "const" in C#.

"Const" keyword is used for making an entity constant.

Example: const string _name = "techstudy"; Once we define a variable as a const then we can’t reassign the value of that variable(string _name).

21) What are Access Modifiers in C#?

Access Modifiers are basically used to control the accessibility of types and members with in the types. In C# there are 5 different types of Access Modifiers.

1. Public: Public is the most common access specifier in C#. It can be accessed from anywhere, that means there is no restriction on accessibility. It is accessible to all classes & projects.

2. Private: The range of the accessibility is limited only within the classes or struct in which they are declared.

3. Protected: All members in the current class & in derived classes can access the variables.

4. Internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

5. Protected Internal: The protected internal access specifier allows a class to hide its member functions & member variables from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.

22) How is the exception handling done in C#?

In C# there is a "try...catch & finally" block to handle the error.

try {
   // statements causing exception
} catch( ExceptionName e1 ) {
   // error handling code
}  finally {
   // statements to be executed
23) Can we execute multiple catch blocks in C#?

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

24) What is Jagged Array in C#?
You can initialize a jagged array as

int[][] marks = new int[2][]{new int[]{12,13,52},new int[]{56,85,45,89}};

Where, marks is an array of two arrays of integers - marks[0] is an array of 3 integers and marks[1] is an array of 4 integers.
25) What is the purpose of using statement in C#?

using keyword is used to include a namespace in the program. A program generally has multiple using statements.

26) How to sort an array in C#?

Using Array.sort(array) function.

27) Is multiple inheritance supported in C#?

No! C# does not support multiple inheritance.

28) What is the difference between "continue" and "break" statements in C#?

Using break statement, you can 'jump out of a loop' while by using continue statement, you can 'jump over one iteration' and then resume your loop execution.

28) What is the difference between while, do while and for loop?

1) While loop checks for the condition first so it may not even enter into the loop if the condition is false.
2) do while loop executes the content of the loop once before checking the condition of the while.
3) For loop is similar to while loop except, the initialization of variable, condition check & increment happens at one place.

29) What is the difference between "constant" and "readonly" variables in C#?

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

30) What is the difference between dispose and finalize methods in c#?

Dispose: This method uses interface – "IDisposable" interface & it will free up both managed and unmanaged codes like - database connection, files etc. It belongs to the IDisposable interface. Implement this method when you are writing a custom class that will be used by other users.

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. Finalize belongs to System.Object class. Implement this method when you have unmanaged resources in your code, and make sure that these resources are freed when the Garbage collection happens.

« 1 2 3 4 5 »


Popular Posts


Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.