C# Interview Questions

C# Interview Questions

1. Explain different states of a Thread in C#?

A thread in C# can have any of the following states:
1. Aborted – The thread is dead but not stopped
2. Running – The thread is executing
3. Stopped – The thread has stopped the execution
4. Suspended – The thread has been suspended


2. What is Multithreading in C#?

Multithreading allows you to perform more than one operation concurrently.The .NET framework System.Threading namespace is used to perform threading in C#


3. Give a brief explanation on Thread Pooling in C#.

A collection of threads, termed as a Thread Pool in C#. Such threads are for performing tasks without disturbing the execution of the primary thread. After a thread belonging to a thread pool completes execution, it returns to the thread pool.


4. Explain Anonymous type in C#?

Anonymous types allow us to create a new type without defining them. This is a way of defining read-only properties into a single object without having to define type explicitly.


5. What do you mean by Generics in C#?

Generic is a class that allows the user to define classes and methods with the placeholder. Generics were added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, etc and user-defined types) to be a parameter to methods, classes, and interfaces.


6. What are collections in C#?

A collection is a set of related objects. It is a class, so you must declare a new collection before you can add elements to that collection.


7. How encapsulation is implemented in C#?

Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member.


8. What is an indexer in C#?

An indexer enables a class or struct instances to be indexed in the same way as an array. It is defined using this keyword.


9. What is the difference between “as” and “is” operators in C#?

1. “as” operator is used for casting the object to type or class.
2. “is” operator is used for checking the object with type and this will return a Boolean value.


10. When you should use the property?

Its uses are:
1. When to validate data before assigning it to a field.
2. When you need to log all access for a child.


11. What is the property in C#?

A property acts as a wrapper around a field. It is used to assign and read the value from that field by using set and get accessors. The property can be created for a public, private, protected and internal field.


12. What are the uses of delegates in C#?

Below is the list of uses of delegates in C# -
1. Callback Mechanism
2. Asynchronous Processing
3. Abstract and Encapsulate method
4. Multicasting


13. What is Method Hiding in C#?

If the derived class doesn't want to use methods in the base class, the derived class can implement its own version of the same method with the same signature. For example, in the classes given below, DriveType() is implemented in the derived class with the same signature. This is called Method Hiding.


14. What is Polymorphism in C#?

The ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. There are two types of polymorphism.
1. Compile-time polymorphism. The best example is Overloading.
2. Runtime polymorphism. The best example is Overriding.


15. What are Delegates?

A delegate is a reference type that holds the reference to a method class method. Any method which has the same signature as a delegate can be assigned to delegate.


16. What are the different types of serialization?

1. XML serialization
2. Binary serialization
3. SOAP serialization


17. What is an exception Handling?

Exception handling is a method to capture run-time errors and handle them correctly. It is done by using Try-catch blocks and throw keyword.


18. What is the role of the System.Exception class?

System.Exception class is provided by the .NET framework to handle any type of exception that occurs. The exception class is the base class for all other exception classes.


19. What do you mean by exceptions in C#?

Exceptions are unexpected or unseen errors that occur during the execution of a program. It can be caused due to the improper use of user inputs, system errors, etc


20. When you should use reflection?

It can be used in the following cases:
1. To create an instance of a type or bind the type to an existing object dynamically.
2. To get the type from an existing object and invoke its methods or access its fields and properties.


21. When you should use reflection?

It can be used in the following cases:


22. What is a reflection in c#?

Reflection is used to examine objects and their types. The system. Reflection namespace contains classes that allow you to obtain information and about assemblies, modules, and types.


23. What are serialization and its main purpose in C#?

Serialization is the process of converting an object into a stream of bytes. That helps in storing that object into memory or a fill int he form of XML, JSON, etc. Its main purpose is to save the state of an object so that it can be recreated when it requires.


24. What are the different ways a method can be overloaded?

Methods can be overloaded using different data types for a parameter, different order of parameters, and the different number of parameters.


25. What do you mean by a partial method?

A partial method is a special method within a partial class or struct. One part of a struct has the only partial method declaration means signature and another part of the same partial class may have an implementation.


Page 1 of 4