Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, 18 August 2017

C#.Net Basic Questions?

What is an IL code?
Why IL code is not fully compiled?
Who compiles the IL code and how does it work?
How does JIT compilationwork?
What are different types of JIT?
What is Native Image Generator (Ngen.exe)?
So does it mean that NGEN.EXE will always improve performance?
What is a CLR?
What is the difference betweenmanaged and unmanaged code?
What is a garbage collector?
What are generations in Garbage collector (Gen 0, 1 and 2)?
Garbage collector cleans managed code, howdo we clean unmanaged code?
But when we create a destructor the performance falls down?
So how can we clean unmanaged objects and also maintain performance?
Can we force garbage collector to run?
What is the difference between finalize and dispose?
What is CTS?
What is a CLS (Common Language Specification)?
What is an Assembly?
What are the different types of Assembly?
What is Namespace?
What is Difference between NameSpace and Assembly?
What is ILDASM?
What is Manifest?
Where is the version information stored of an assembly?
Is versioning applicable to private assemblies?
What is the use of strong names?
What is Delay signing?
What is GAC?
How to add and remove an assembly from GAC?
If we have two versions of the same assembly in GAC how to we make a choice?
What is reflection?
What are stack and heap?
What are Value types and Reference types?
What is concept of Boxing and Unboxing?
How performance is affected due to boxing and unboxing?
How can we avoid boxing and unboxing?
How to prevent my .NET DLL to be decompiled?
What is the difference between Convert.toString and .toString () method?
How can we handle exceptions in .NET?
How can I know from which source the exception occurred?
What if we do not catch the exception?
What are system level exceptions and application level exceptions?
Can two catch blocks be executed?
What are different types of collections in .NET?
What is the difference between arraylist and list?
Are Arraylist faster or Arrays?
What are hashtable collections?
What are Queues and stack collection?
Can you explain generics in .NET?
Can you explain the concept of generic collection?
What is the difference between dictionary and hashtable?
What are the generic equivalent for array list,stack, queues and hashtable?
What is the use of IEnumerable, ICollection, Ilist and IDictionary?
What is code access security (CAS)?
So how does CAS actually work?
Is CAS supported in .NET 4.0?
What is sandboxing?
How can we create a windows service using .NET?
What is serialization and deserialization in .NET?
Can you mention some scenarios where we can use serialization?
When should we use binary serialization as compared to XML serialization?
Can you explain the concept of “Short Circuiting”?
What is the difference between “Typeof” and “GetType”?
Will the following c# code compile?

Saturday, 14 January 2017

Virtual vs Overriding vs New keyword in c#?

Virtual and Overriding keyword are used for method overriding. Method overriding is concept of Polymorphism. New keyword is used for method hiding.
Now we check how to use this keyword in c# by example.

Example 1:-

namespace ConsoleProg
{
    class A
    {
        public void show()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public void show()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public void show()
        {
            Console.WriteLine("C");
        }
    }
    class Program
    {
        static void Main()
        {
            A a = new A();
            a.show();   // print-- A
            B b = new B();
            b.show();  // print-- B
            C c = new C();
            c.show(); // print-- C

            A a1 = new B();
            a1.show(); // print-- A
        }
    }
}
Note:- But it show some warning message.

1.) 'ConsoleProg.B.show()' hides inherited member 'ConsoleProg.A.show()'. Use the new keyword if hiding was intended.
 2.) ‘ConsoleProg.C.show()' hides inherited member 'ConsoleProg.B.show()'. Use the new keyword if hiding was intended.  
Solution of the above warning is use either new keyword or overriding keyword in derived class in method.

Virtual And Overriding keyword (Method overriding):-

If you want to be method overriding in the derived class, then the base class method must have the virtual keyword otherwise it will throw an error.

Example 2:-

namespace ConsoleProg
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public override void show()
        {
            Console.WriteLine("C");
        }
    }
    class Program
    {
        static void Main()
        {
            A a = new A();
            a.show();   // print-- A
            B b = new B();
            b.show();  // print-- B
            C c = new C();
            c.show(); // print-- C

            A a1 = new B();
            a1.show(); // print-- B
        }
    }
}

New Keyword (Method hiding):

New keyword is used for method hiding the base class method from derived class.

Example 3:-

namespace ConsoleProg
{
    class A
    {
        public void show()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public new void show()
        {
            Console.WriteLine("B");
        }
    }
    class Program
    {
        static void Main()
        {
            A a = new A();
            a.show();   // print-- A
            B b = new B();
            b.show();  // print-- B

            A a1 = new B();
            a1.show(); // print-- B
        }
    }
}

Method Overriding and Hiding:-

Example 4:-

 namespace ConsoleProg
{
    class A
    {
        public  void show()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public new virtual void show()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public override void show()
        {
            Console.WriteLine("C");
        }
    }
    class Program
    {
        static void Main()
        {
            A a = new A();
            a.show();   // print-- A
            B b = new B();
            b.show();  // print-- B
            C c = new C();
            c.show(); // print-- C

            A a1 = new B();
            a1.show(); // print-- A

            B b2 = new C();
            b2.show();  // print-- C

            A a2 = new C();
            a2.show();// print-- A
        }
    }
}

Example 5:

namespace ConsoleProg
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public override void show()
        {
            Console.WriteLine("C");
        }
    }
    class Program
    {
        static void Main()
        {
            A a = new A();
            a.show();   // print-- A
            B b = new B();
            b.show();  // print-- B
            C c = new C();
            c.show(); // print-- C

            A a1 = new B();
            a1.show(); // print-- A

            B b2 = new C();
            b2.show();// print-- C

            A a2 = new C();
            a2.show();// print-- C
        }
    }
}
Example 6:
namespace ConsoleProg
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public new void show()
        {
            Console.WriteLine("C");
        }
    }
    class Program
    {
        static void Main()
        {
            A a = new A();
            a.show();   // print-- A
            B b = new B();
            b.show();  // print-- B
            C c = new C();
            c.show(); // print-- C

            A a1 = new B();
            a1.show(); // print-- A

            B b2 = new C();
            b2.show(); // print-- B

            A a2 = new C();
            a2.show(); // print-- B
        }
    }
}

Important Point:

Override:-
      1.) Use this keyword polymorphism implementation along with virtual.  
      2.) With same name and parameter as in base class.
      3.) It is call called as run time polymorphism.
      4.) Also called as late binding.

New :-
     1.) It is used in polymorphism concept.
     2.) With same name and different parameters.  
     3.) Compile time bindig.
     4.) Also called early binding.