Sunday, August 16, 2015

Can you inherit multiple interfaces in C#?

Implement Multiple Interfaces

Yes, we can implement multiple interfaces in one class. This is only the way to achieve multiple inheritances in C#.

Example:

       public interface MyInterface1
    {
        int Add(int i, int j);
    }

    public interface MyInterface2
    {
        int Sub(int i, int j);
    }

    public class Math : MyInterface1, MyInterface2
    {
        public int Add(int i, int j)
        {
            return i + j;
        }

        public int Sub(int i, int j)
        {
            return i - j;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Math obj = new Math();

            int i = obj.Add(20, 10);
            Console.WriteLine(i);

            int j = obj.Sub(20, 10);
            Console.WriteLine(j);

        }

    }

Can we write Return statement in finally block in c#?

Return statement in finally block

No. we can not write Return statement in finally block in c#. if you will write it, application will give you compilation error.

Example:

try
{
Return 1;
}
Catch(Exception ex)
{
Return 2;
}
Finally
{
Return 3; // Compilation error
}


It will give you compilation error as you cannot write “Return” statement in finally block.

Difference between Deferred Execution and Immediate Execution in c#?


Deferred Execution

LINQ query has three operations to execute:
  1. Get the data source
  2. Create the LINQ query
  3. Execute the query

Example:
        static void Main()
        {
            List<int> Number = new List<int> { 500, 400, 800, 620, 720};
            IEnumerable<int> desNumber = from i in Number
                                               orderby i
                                               select i;

            //Execute Query (Deferred Execution)
            foreach (int number in desNumber)
            {
                Console.WriteLine(number);
            }

            Console.ReadLine();
        }

Result:
400
500
620
720
800

The above LINQ query execution is called deferred execution.


Immediate Execution

Sometime you need to get the number count or min max number you have to use Count, MIN and MAX method. This execution is called immediate execution.

Example:
        static void Main()
        {
List<int> Number = new List<int> { 500, 400, 800, 620, 720};
            IEnumerable<int> desNumber = from i in Number
                                               orderby i
                                               select i;
            //Immediate Execution
            int maxNumber = desNumber.Max();
            Console.WriteLine(maxNumber);

            Console.ReadLine();
        }

Result:

800

Different between copy and clone in c#?


Copy and Clone

Clone:-

It only copies structure, does not copy data. Creates another reference (Pointer) to the same location.

Example 1-

dt=ds.Table[0].clone(); //will create only the structure of table not data.

Example 2-

private void GetClone(DataTable table) { // Get a clone of the original DataTable. DataTable cloneTable; cloneTable = table.Clone(); }

Copy:- 

Copies both Structure and data. Creates two different references to two different Address Spaces.
copy is deep copy. copy the structure and data of the object.

Example

DataTable dt=new DataTable(); dt=ds.Tables[0].copy(); // Will copy all the data and structure to dt table Dataset .Copy: - It copies both structure and data. Dataset .Clone: - It only copies structure, does not copy data.


Difference between Var and Dynamic type in c#

Var data type was introduced in .NET 3.5 with Visual Studio 2008 and Dynamic was introduce in .NET 4.0 with Visual Studio 2010.


Var Data Type:


Var declaration is resolved at compile time that means the type of variable declared is decided by the compiler at compile time.

Example: 

Var name = “Sachin is in India”; // Correct
Name =2; // Compilation error  

In this example, compiler set the name variable as string type at compile time. If you will assign another type value in “name” variable, it will give compilation error.

Please note that as var is compile time therefore you cannot leave it as blank. You have to assign some value at compile tome only.


Dynamic Data Type:

Dynamin declaration is resolved at Run time that means the type of variable declared is decided by the compiler at Run time.

Example: 

Dynamic name = “Sachin is in India”; // Correct
Name =2; // Correct

In this example, compiler set the name variable as string type at Run time. If you will assign another type value in “name” variable, it will not give you any error and change the type of variable at run time.

Please note that any error in dynamic variable will come only at run time because compiler does not understand the type of variable at compile time. It is not necessary that you will assign some value to Dynamic variable at compile time.

You can declare Dynamic data type like this:
Dynamic str;
Str=”Sachin”;
Str = 10;


Note: It is highly recommended that we should avoid using Dynamic as it does Boxing\UnBoxing and impact on application performance. We should use Dynamic only when it is really required.

Sunday, January 20, 2013

Difference between Jagged Array and Multi Dimension Array in C#?


Jagged Array

A jagged array is an array whose element is arrays and the element of jagged array could be of different size and dimension.

Multi Dimension Array

A multi dimension array is an array whose element will be of same size.


Jagged Array Example


       
     
         
   

3-Dimension Array Example


     
     
     


Example

    class JaggedArray
    {
        static void Main()
        {
            // Declare jagged array of size 4.
            int[][] objJaggedArray = new int[4][];

            // Initialization jagged array.
            objJaggedArray[0] = new int[4];
            objJaggedArray[1] = new int[3];
            objJaggedArray[2] = new int[5];
            objJaggedArray[3] = new int[2];


            objJaggedArray[0] = new int[] { 9, 8, 7, 6 };
            objJaggedArray[1] = new int[] { 9, 8, 7 };
            objJaggedArray[2] = new int[] { 9, 8, 7, 6, 5 };
            objJaggedArray[3] = new int[] { 9, 8 };

            Console.WriteLine("Jagged Array Items...");

           // Display the jagged array elements:              
            for (int i = 0; i < objJaggedArray.Length; i++)
            {
                Console.WriteLine();
                for (int j = 0; j < objJaggedArray[i].Length; j++)
                {
                    Console.Write(objJaggedArray[i][j]+ " ");
                }
            }

            Console.WriteLine();

            int[,] myArray = new int[3, 3];
            myArray[0, 0] = 1;
            myArray[0, 1] = 2;
            myArray[0, 2] = 3;
            myArray[1, 0] = 4;
            myArray[1, 1] = 5;
            myArray[1, 2] = 6;
            myArray[2, 0] = 7;
            myArray[2, 1] = 8;
            myArray[2, 2] = 9;

            Console.WriteLine("3D Array Items...");

            // Display the 2D array elements:             
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(myArray[i, j] + " ");
                }
                Console.WriteLine();
            }            


            Console.ReadLine(); 

        }
    }
            

Output

Jagged Array Items...
9 8 7 6
9 8 7
9 8 7 6 5
9 8

3D Array Items...
1 2 3
4 5 6
7 8 9

What is the top .NET class that everything is derived from?


Top .NET class

Every class in .NET inherit from System.Object class. To make sure, just create a blank class and create an object of this class. System.Object class contains four default methods and these method can be accessed by any of the class object. It prove that all the classes are inherit from System.Object class.

Methods of System.Object Class:


  • Equals
  • GetHashCode
  • GetType
  • ToSting


Example

    class abc          
    {
        public string MyMethod()
        {
            return "Welcome";
        }
    }  

     class Program
     {
        static void Main(string[] args)
         {
            abc obj = new abc();
            Console.WriteLine("abc class method MyMethod() - " + obj.MyMethod());
            Console.WriteLine("System.Object class method GetHashCode()- " + obj.GetHashCode());
            Console.WriteLine("System.Object class method GetType()- " + obj.GetType());
            Console.WriteLine("System.Object class method ToString()- " + obj.ToString());
            Console.ReadLine();
         }
     }

Output

abc class method MyMethod() – Welcome
System.Object class method GetHashCode()- 45653674
System.Object class method GetType()- ConsoleApplication1.abc
System.Object class method ToString()-ConsoleApplication1.abc

Note.The above code is showing that the abc class contain only one method but we are accessing more methods like GetHashCode, GetType etc. so these methods are coming from base class which is System.Object