C# / .NET Interview Questions

- Manage languages
- Complier, MSIL, CRL JIT, Garbage collector 
- Class vs object
- Value type vs Reference type
- Const vs readonly
- Out vs Ref
- Struct vs class
- Enum
- Constructor
- Static members vs instance members
- Partial class
- Object initializer/ Collection initializer
- Override vs overload
- Virtual/override vs new
- Abstract vs interface
- Generic
- Delegate
- Event
- Immutable
- Automatic property
- Anonymous method
- Anonymous type
- Extension method
- Optional parameter
- Lamda expression
- Var vs dynamic
- Async
- Exception handling
- Refection
- Attribute


1. What is the difference between arrays and collection?

Array:
You need to specify the size of an array at the time of its declaration. It cannot be resized dynamically.
The members of an array should be of the same data type.

Collection:
The size of a collection can be adjusted dynamically, as per the user's requirement. It does not have fixed size.

Collection can have elements of different types.

2. What are collections and generics?

A collection can be defined as a group of related items that can be referred to as a single unit. The System.Collections namespace provides you with many classes and interfaces. Some of them are - ArrayList, List, Stack, ICollection, IEnumerable, and IDictionary. Generics provide the type-safety to your class at the compile time. While creating a data structure, you never need to specify the data type at the time of declaration. The System.Collections.Generic namespace contains all the generic collections.

3. What is the index value of the first element in an array?

In an array, the index value of the first element is 0 (zero).

4. What is a multicast delegate?

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

5. Define enumeration?

Enumeration is defined as a value type that consists of a set of named values. These values are constants and are called enumerators. An enumeration type is declared using the enum keyword. Each enumerator in an enumeration is associated with an underlying type that is set, by default, on the enumerator. The following is an example that creates an enumeration to store different varieties of fruits:

enum Fruits {Mango, Apple, orange, Guava};

In the preceding example, an enumeration Fruits is created, where number 0 is associated with Mango, number 1 with Apple, number 2 with Orange, and number 3 with Guava. You can access the enumerators of an enumeration by these values.

6. In which namespace, all .NET collection classes are contained?

The System.Collections namespace contains all the collection classes.

7. How has exception handling changed in .NET Framework 4.0?

In .NET 4.0, a new namespace, System.Runtime.ExceptionServices, has been introduced which contains the following classes for handling exceptions in a better and advanced manner:

HandleProcessCorruptedStateExceptionsAttribute Class - Enables managed code to handle the corrupted state exceptions that occur in an operating system. These exceptions cannot be caught by specifying the try...catch block. To handle such exceptions, you can apply this attribute to the method that is assigned to handle these exceptions.

FirstChanceExceptionEventArgs Class - Generates an event whenever a managed exception first occurs in your code, before the common language runtime begins searching for event handlers.

8. What is a delegate?

A delegate is similar to a class that is used for storing the reference to a method and invoking that method at runtime, as required. A delegate can hold the reference of only those methods whose signatures are same as that of the delegate. Some of the examples of delegates are type-safe functions, pointers, or callbacks.

9. Can you use the 'throws' clause to raise an exception?

No, the throws clause cannot be used to raise an exception. The throw statement signals the occurrence of an exception during the execution of a program. When the program encounters a throw statement, the method terminates and returns the error to the calling method.

10. Define an array.

An array is defined as a homogeneous collection of elements, stored at contiguous memory locations, which can be referred by the same variable name. All the elements of an array variable can be accessed by index values. An Index value specifies the position of a particular element in an array variable.

11. What is a hashtable?

Hashtable is a data structure that implements the IDictionary interface. It is used to store multiple items and each of these items is associated with a unique string key. Each item can be accessed using the key associated with it. In short, hashtable is an object holding the key-value pairs.

12. What are queues and stacks?

Stacks refer to a list in which all items are accessed and processed on the Last-In-First-Out (LIFO) basis. In a stack, elements are inserted (push operation) and deleted (pop operation) from the same end called top.

Queues refer to a list in which insertion and deletion of an item is done on the First-In-First-Out (FIFO) basis. The items in a queue are inserted from the one end, called the rear end, and are deleted from the other end, called the front end of the queue.

13. Define an event.

Whenever an action takes place in a class, that class provides a notification to other classes or objects that are assigned to perform particular tasks. These notifications are called events. For example, when a button is clicked, the class generates an event called Click. An event can be declared with the help of the event keyword.

Không có nhận xét nào :

Đăng nhận xét