1 - How C# compilation process?


The C# compiler compiles the C# source code into modules, which is finally converted into the assembly. 

The assembly contains the Intermediate Language (IL) code along with the metadata information about the assembly.

The common language runtime (CLR) works with the assembly. It loads the assembly and converts it into the native code to execute the assembly. 

Then this native code is executed by the Operating system and the output will shows according to your requirement.

2 - Can "this" be used within a static method?


No, we can't use "this" in static method because keyword this returns a reference to the current instance of the class. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class and call with the name of a class not by instance so we can't use this keyword in the body of static Methods.

3 - What is the difference between Interface and Abstract Class?


Abstract class is a way to achieve the abstraction in C# and is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword abstract in the class definition. The Abstract classes are typically used to define a base class in the class hierarchy.

Interfaces can have methods, properties, events as its members and it will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly. Also, a class can implement multiple interfaces.

4 - What is sealed class in C#?


Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited. Structs are also sealed. You cannot derive a class from a struct.

5 - What is the difference between overloading and overriding?


Overloading is when you have multiple methods in the same scope, with the same name but different signatures.

Overriding is a principle that allows you to change the functionality of a method in a child class.

6 - What are the uses of “using” in C#?


The reason for the using statement is to ensure that the object is disposed (call IDisposable) as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.

7 - What are generics in C#?


Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.

You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type.

8 - What are partial classes?


A partial class is only use to split the definition of a class in two or more classes in a same source code file or more than one source files. You can create a class definition in multiple files but it will be compiled as one class at run time and also when you'll create an instance of this class so you can access all the methods from all source file with a same object. Partial classes can be create in the same namespace it's doesn't allowed to create a partial class in different namespace.

9 - What is the difference between Equality Operator (==) and Equals() Method in C#?


The == Operator (usually means the same as ReferenceEquals, could be overrided) compares the reference identity while the Equals() (virtual Equals()) method compares if two objects are equivalent.

10 - What is Virtual Method in C#?


A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation in a base class as well as derived class. When a virtual method is invoked, the run-time type of the object is checked for an overriding member. By default, methods are non-virtual. We can't override a non-virtual method.

11 - What is the difference between ref and out keywords?


  • The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed

  • The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method.

12 - Give a brief explanation on Thread Pooling in C#


A collection of threads is termed as a Thread Pool in C#. Such threads can be used 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.

13 - What are the async and await keywords?


The async keyword enables the await keyword in that method and changes how method results are handled. The method runs synchronously until it hits an await.

The “await” keyword examines an awaitable to see if it has already completed. if the awaitable has already completed, then the method just continues running synchronously. If await sees that the awaitable has not completed, then it acts asynchronously. It tells the awaitable to run the remainder of the method when it completes, and then returns from the async method. If you’re awaiting a built-in awaitable (such as a Task), then the remainder of the async method will execute on a context that was captured before the await returned.

14 - What is Boxing and Unboxing?


Boxing and Unboxing both are used for type conversion but have some difference:

  • 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 is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.

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

    int i = 123;

    object o = i; // boxing 

    o = 123;

    i = (int)o;  // unboxing

15 - What you understand by Value types and Reference types in C#.Net?


In C# data types can be of two types: Value Types and Reference Types.

Value type variables contain their object (or data) directly. If we copy one value type variable to another then we are actually making a copy of the object for the second variable. Value Type member will located into Stack and reference types will located in Heap always.

16 - Explain what is weak reference in C#?


The garbage collector cannot collect an object in use by an application while the application's code can reach that object. The application is said to have a strong reference to the object.

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. 

A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

Weak references are useful for objects that use a lot of memory, but can be recreated easily if they are reclaimed by garbage collection.

17 - What are the differences between Tasks And Threads?


A Thread represents an actual OS-level thread, with its own stack and kernel resources. Each thread you have consumes a non-trivial amount of memory for its stack, and adds additional CPU overhead as the processor context-switch between threads. Instead, it is better to have a small pool of threads execute your code as work becomes available.

The Task object is the main concept of the Task Parallel Library (TPL). Which represents a unit of work being done that will eventually return to the caller. The work performed by a Task executes asynchronously on a thread pool thread.

18 - What is difference between constants and readonly?


Constant variables are declared and initialised at compile time. The value can't be changed afterwards. Readonly is used only when we want to assign the value at run time.

19 - What is the difference between structs and classes?


Structs are value types and classes are reference types. The main difference is that a reference type lives on the heap, and a value type lives on the stack. This generally makes structs cheaper to allocate or deallocate than classes. Reference types are allocated on the heap and garbage-collected, which tends to take up more resources. Structs are more limited than classes and do not support inheritance. Also since they are value types are copied by value which may cause some performance issues if it's a big struct.