C#

Generics

A generic collection is strongly typed (type safe), meaning that you can only put one type of object into it. This eliminates type mismatches at runtime. Another benefit of type safety is that performance is better with value type objects because they don't incur overhead of being converted to and from type object

Delegates

A delegate(known as function pointer in C/C++) is a references type that invokes single/multiple method(s) through the delegate instance. It holds a reference of the methods. Delegate types are sealed and immutable type.

delegate int NumberChanger(int n);namespace DelegateAppl{    class TestDelegate    {       static int num = 10;       public static int AddNum(int p)       {          num += p;          return num;       }        public static int MultNum(int q)       {          num *= q;          return num;       }       public static int getNum()       {          return num;       }        static void Main(string[] args)       {          //create delegate instances          NumberChanger nc1 = new NumberChanger(AddNum);          NumberChanger nc2 = new NumberChanger(MultNum);                    //calling the methods using the delegate objects          nc1(25);          Console.WriteLine("Value of Num: {0}", getNum());          nc2(5);          Console.WriteLine("Value of Num: {0}", getNum());          Console.ReadKey();       }    }}

delegate int NumberChanger(int n);namespace DelegateAppl{    class TestDelegate    {       static int num = 10;       public static int AddNum(int p)       {          num += p;          return num;       }        public static int MultNum(int q)       {          num *= q;          return num;       }              public static int getNum()       {          return num;       }        static void Main(string[] args)       {          //create delegate instances          NumberChanger nc;          NumberChanger nc1 = new NumberChanger(AddNum);          NumberChanger nc2 = new NumberChanger(MultNum);          nc = nc1;          nc += nc2;                    //calling multicast          nc(5);          Console.WriteLine("Value of Num: {0}", getNum());          Console.ReadKey();       }    }}

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.

Async and await

How Requests Are Processed by the Thread Pool

On the web server, the .NET Framework maintains a pool of threads that are used to service ASP.NET requests. When a request arrives, a thread from the pool is dispatched to process that request. If the request is processed synchronously, the thread that processes the request is busy while the request is being processed, and that thread cannot service another request.

This might not be a problem, because the thread pool can be made large enough to accommodate many busy threads. However, the number of threads in the thread pool is limited (the default maximum for .NET 4.5 is 5,000). In large applications with high concurrency of long-running requests, all available threads might be busy. This condition is known as thread starvation. When this condition is reached, the web server queues requests. If the request queue becomes full, the web server rejects requests with an HTTP 503 status (Server Too Busy). The CLR thread pool has limitations on new thread injections. If concurrency is bursty (that is, your web site can suddenly get a large number of requests) and all available request threads are busy because of backend calls with high latency, the limited thread injection rate can make your application respond very poorly. Additionally, each new thread added to the thread pool has overhead (such as 1 MB of stack memory). A web application using synchronous methods to service high latency calls where the thread pool grows to the .NET 4.5 default maximum of 5, 000 threads would consume approximately 5 GB more memory than an application able the service the same requests using asynchronous methods and only 50 threads. When you're doing asynchronous work, you're not always using a thread. For example, when you make an asynchronous web service request, ASP.NET will not be using any threads between the asyncmethod call and the await. Using the thread pool to service requests with high latency can lead to a large memory footprint and poor utilization of the server hardware.

Processing Asynchronous Requests

In web applications that sees a large number of concurrent requests at start-up or has a bursty load (where concurrency increases suddenly), making these web service calls asynchronous will increase the responsiveness of your application. An asynchronous request takes the same amount of time to process as a synchronous request. For example, if a request makes a web service call that requires two seconds to complete, the request takes two seconds whether it is performed synchronously or asynchronously. However, during an asynchronous call, a thread is not blocked from responding to other requests while it waits for the first request to complete. Therefore, asynchronous requests prevent request queuing and thread pool growth when there are many concurrent requests that invoke long-running operations.

Choosing Synchronous or Asynchronous Action Methods

This section lists guidelines for when to use synchronous or asynchronous action methods. These are just guidelines; examine each application individually to determine whether asynchronous methods help with performance.

In general, use synchronous methods for the following conditions:

C#

namespace AsyncAwaitExample {     class Program     {         static void Main()         {             var demo = new AsyncAwaitDemo();             demo.DoStuff();              while (AsyncAwaitDemo.vari)             {                 Console.WriteLine("Doing Stuff on the Main Thread...................");             }             Console.ReadLine();         }     }      public class AsyncAwaitDemo     {         public static bool vari = true;          public async Task DoStuff()         {             await Task.Run(() =>             {                 LongRunningOperation();             });         }          private static async Task<string> LongRunningOperation()         {             int counter;              for (counter = 0; counter < 50000; counter++)             {                 Console.WriteLine(counter);             }              if (counter == 50000)             {                 vari = false;             }             return "Counter = " + counter;         }     } }

static void Main(string[] args) {

            var read1 = ReadFileAsync(@"c:\somefile.txt ");

            var read2 = ReadFileAsync(@"c:\someotherfile.txt ");

            Task.WhenAll(read1, read2)

               .ContinueWith(task => Console.WriteLine("All files have been read successfully."));   

            //do other work here while files are read...

           Console.ReadLine();

        }

Using Delegates with Events

The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the even

Passing Reference-Type Parameters by Val

The following example demonstrates passing a reference-type parameter, arr, by value, to a method, Change. Because the parameter is a reference to arr, it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable, arr.

C#

class PassingRefByVal  {     static void Change(int[] pArray)     {         pArray[0] = 888;  // This change affects the original element.         pArray = new int[5] {-3, -1, -2, -3, -4};   // This change is local.         System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);     }      static void Main()      {         int[] arr = {1, 4, 5};         System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);          Change(arr);         System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);     } } /* Output:     Inside Main, before calling the method, the first element is: 1     Inside the method, the first element is: -3     Inside Main, after calling the method, the first element is: 888 */

Passing Reference-Type Parameters by Ref

The following example is the same as the previous example, except that the ref keyword is added to the method header and call. Any changes that take place in the method affect the original variable in the calling program.

C#

class PassingRefByRef  {     static void Change(ref int[] pArray)     {         // Both of the following changes will affect the original variables:         pArray[0] = 888;         pArray = new int[5] {-3, -1, -2, -3, -4};         System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);     }      static void Main()      {         int[] arr = {1, 4, 5};         System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);          Change(ref arr);         System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);     } } /* Output:     Inside Main, before calling the method, the first element is: 1     Inside the method, the first element is: -3     Inside Main, after calling the method, the first element is: -3 */

Swapping strings is a good example of passing reference-type parameters by reference. In the example, two strings,str1 and str2, are initialized in Main and passed to the SwapStrings method as parameters modified by the refkeyword. The two strings are swapped inside the method and inside Main as well.

C#

 namespace ConsoleApplication1 {     class Person     {        public int age;     }      class Program     {         static void Square(Person a, Person b)         {             a.age = a.age * a.age;              b.age = b.age * b.age;              Console.WriteLine(a.age + " " + b.age);         }          static void Main(string[] args)         {             Person p1 = new Person();             Person p2 = new Person();              p1.age = 5;             p2.age = 10;              Console.WriteLine(p1.age + " " + p2.age);              Square(p1, p2);              Console.WriteLine(p1.age + " " + p2.age);             Console.ReadLine();         }     } }  ////The output would look like this: ////5 10 ////25 100 ////25 100

Difference between For and Foreach Loop in C#

This code will not compile. I have pasted screenshot of error.

foreach

Foreach loop

Foreach loop runs upon a single thread and processing takes place sequentially one by one. Foreachloop is a basic feature of C# and it is available from C# 1.0. Its execution is slower than Parallel.Foreach in most of the cases.

Parallel.ForEach loop

Parallel.ForEach loop runs upon multiple threads and processing takes place in a  parallel way. Parallel.ForEach loop is not a basic feature of C# and it is available from C# 4.0 and above. Before C# 4.0 we cannot use it. Its execution is faster than foreach in most of the cases. To use Parallel.ForEach loop we need to import System.Threading.Tasks namespace in using directive.

Difference between destructor, dispose and finalize method?

Destructors 

For example, the following is a declaration of a destructor for the class Car:

C#

class Car {     ~Car()  // destructor     {         // cleanup statements...     } } 

The destructor implicitly calls Finalize on the base class of the object. Therefore, the previous destructor code is implicitly translated to the following code:

protected override void Finalize() {     try     {         // Cleanup statements...     }     finally     {         base.Finalize();     } } 

This means that the Finalize method is called recursively for all instances in the inheritance chain, from the most-derived to the least-derived.

Static Constructors ,Private Constructors :

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:

C#

class NLog {     // Private Constructor:      private NLog() { }      public static double e = Math.E;  //2.71828... } 

The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you do not use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static. 

// If you uncomment the following statement, it will generate          // an error because the constructor is inaccessible:          // NLog obj= new NLog ();   // Error

 But, if a class has other public constructors, then that can be instantiated. A class can have multiple private constructor and can call it by another constructor.

For example:- 

Class A

{

      int a;

      private A()

      {              

       }

       public A(int b) : A()  // Calling private constructor by another constructor.

       {

                this.a=b;

        }

}

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

C#

class SimpleClass {     // Static variable that must be initialized at run time.      static readonly long baseline;      // Static constructor is called at most one time, before any      // instance constructor is invoked or member is accessed.      static SimpleClass()     {         baseline = DateTime.Now.Ticks;     } } 

Static constructors have the following properties:

What is the difference between final, finally and finalize?

Final:- It is used in the following cases:

Finally:-

If an exception is thrown in try block then the control directly passes to the catch block without executing the lines of code written in the remainder section of the try block. 

In case of an exception we may need to clean up some objects that we created. If we do the clean-up in try block, they may not be executed in case of an exception. 

Thus finally block is used which contains the code for clean-up and is always executed after the try ...catch block.

Finalize:-

It is a method present in a class which is called before any of its object is reclaimed by the garbage collector. finalize() method is used for performing code clean-up before the object is reclaimed by the garbage collector.

Constant

Constant fields or local variables must be assigned a value at the time of declaration and after that they cannot be modified. By default constant are static, hence you cannot define a constant type as static.

ReadOnly

A readonly field can be initialized either at the time of declaration or with in the constructor of same class. Therefore, readonly fields can be used for run-time constants.

Static

The static keyword is used to specify a static member, which means static members are common to all the objects and they do not tied to a specific object. This keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.

Key points about Static keyword

New Keyword

New keyword is also used in polymorphism concept, but in the case of method overloading So what does overloading means, in simple words we can say procedure of hiding your base class through your derived class.

To hide base class methods in derived classes without having any warning messages we can declare derived class methods with new keyword.

It is implemented as:

Hide   Copy Code

class A     {         public void show()         {             Console.WriteLine("Hello: Base Class!");             Console.ReadLine();         }     }      class B : A     {         public new void show()         {             Console.WriteLine("Hello: Derived Class!");             Console.ReadLine();         }     }

Virtual Keyword

Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. Virtual keyword is used within a set with override keyword. It is used as:

Override Keyword

Override keyword is used in the derived class of the base class in order to override the base class method.Override keyword is used with virtual keyword, as:

// Base Class     class A     {         public virtual void show()         {             Console.WriteLine("Hello: Base Class!");             Console.ReadLine();         }     }  // Derived Class      class B : A     {         public override void show()         {             Console.WriteLine("Hello: Derived Class!");             Console.ReadLine();         }     }

Static Class: Declared with Static keyword, methods in Static Class are also static along with variables of the class.This class cannot be instantiated, 

i.e we cannot have objects of this class. To access methods of this class, you can directly use classname.method. Also this class cannot be inherited.

Sealed Class: Declared with Sealed keyword, which enables this class to seal all its variables, methods and properties. No other class can inherit anything from this class or in other words, 

this class cannot be inherited. But we can instantiate this class, i.e we can have any number of objects of a sealed class. sealed class can not be inherited in other words, no class can be derived from the sealed class. 

Basically used for commercial reasons where restrictions are implemented on the class so that no one use the existing class and derive from it without the license.

If a method is declared as sealed it can not be overridden. Used to avoid instability of some major code.

Abstract Class : A class that contains atleast one abstract method and uses a keyword abstract is a abstract class. It may or may not have the non abstract methods. This class can be inherited.

Abstract method means that the method does not have any implementation..it just contains the signature of method. The abstract method is required to be implemented in the derived class.

Partial Class 

A class defined in  two or more files is called a partial class. The keyword partial is used to define the class. When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously. During compile time all the partial class are compiled into one type only.

IEnumerable Interface : Exposes an enumerator, which supports a simple iteration over a non-generic collection.

ICollection inherits from IEnumerable. There is one difference:

you can find IEnumerable[ i ] --> Index Based

you can NOT find ICollection[ i ] --> Not Index Based

IEnumerable interface is used when we want to iterate among our classes using a foreach loop. The IEnumerable interface has one method, GetEnumerator, that returns an IEnumerator interface that helps us to iterate among the class using the foreach loop. 

It provides read-only access to collections. We cannot change any item inside an IEnumerable List.

ICollection Interface

Defines methods to manipulate generic collections.The ICollection interface is inherited from the IEnumerable interface which means that any class that implements the ICollection interface can also be enumerated using a foreach loop. In the IEnumerable interface we don't know how many elements there are in the collection whereas the ICollection interface gives us this extra property for getting the count of items in the collection. The ICollection interface contains the following: 

The Count property is used for maintaining the count of elements in the list whereas the IsSysnchronized and SyncRoot properties help to make the collection thread-safe. The CopyTo method copies the entire collection into an array. 

The generic version of this interface provides Add and Remove methods also. 

IList Interface

The IList interface implements both ICollection and IEnumerable interfaces. This interface allows us to add items to and remove items from the collection. It also provides support for accessing the items from the index. This interface has more power than the preceding two interfaces.The IList interface has one indexer by which we can access any element by its position and can insert an element and remove an element at any position.

//IList {indexer and Modify} vs ICollection {randomly and Modify}  //Collection can not be instantiate from ICollection , so it should be instantiate from List  System.Collections.Generic.ICollection<string> strICollection = new List<string>();

yield 

“Yield keyword helps us to do custom stateful iteration over .NET collections.”

There are two scenarios where “yield” keyword is useful:-

You consume an iterator method by using a foreach statement or LINQ query. Each iteration of the foreach loop calls the iterator method. When a yield return statement is reached in the iterator method, expression is returned, and the current location in code is retained. Execution is restarted from that location the next time that the iterator function is called.

C#

public class PowersOf2 {     static void Main()     {         // Display powers of 2 up to the exponent of 8:          foreach (int i in Power(2, 8))         {             Console.Write("{0} ", i);         }     }      public static System.Collections.Generic.IEnumerable<int> Power(int number, int exponent)     {         int result = 1;          for (int i = 0; i < exponent; i++)         {             result = result * number;             yield return result;         }     }      // Output: 2 4 8 16 32 64 128 256 }