Search This Blog

Q6-Q10

Q6. When we should use Abstract class instead of interface?
Q7. What is the use of using block?
Q8. What is polymorphism. What are different types of polymorphism in C#?
Q9. Few difference between var and Dynamic keyword in C#?
Q10. Can we put different types in Dynamic list at run time. example 1st we put customer class and then we put seller class in C#?
--------------------------------------------------------------------------------------------------------------------------
Q6. When we should use Abstract class instead of interface?

Answer:
  1. You have default or shared code implementation that you want in your child class
  2. You want to minimize code duplication
  3. Multiple inheritance is not a concern.
--------------------------------------------------------------------------------------------------------------------------
Q7. What is the use of using block?

Answer:
Using block helps to free un-used memory by implementing IDisposable
Using block is used when we are working on un-managed code like fetching data from database, file stream like reading files etc.

--------------------------------------------------------------------------------------------------------------------------
Q8. What is polymorphism. What are different types of polymorphism in C#?

Answer:
Polymorphism means one name many forms. 
There are two types of polymorphism

  1. Compile time Polymorphism: It is also known as static polymorphism or early binding polymorphism. C# has two techniques of implementing static polymorphism.  
    1. Function Overloading - In function overloading you can have multiple definitions for the same function name in the same scope. 
      1. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list or by arrangement of arguments. 
      2. Different return type and same method name is not functional overloading and matter of fact, methods with same name different return types are NOT allowed in C#.
    2. Operator OverloadingOperator overloading gives the ability to use the same operator to do various operations
  2. Run time Polymorphism: It is also known as Dynamic polymorphism or late binding polymorphism. This can be achieved by using virtual & override keywords. Also Abstract and override in case of abtract class. 
Example of Run time polymorphism:

// Base Class
public class Users
{
    public virtual void GetInfo()
    {
        Console.WriteLine("Base Class");
    }
}

// Derived Class
public class Details : Users
{
    public override void GetInfo()
    {
        Console.WriteLine("Derived Class");
    }
}
--------------------------------------------------------------------------------------------------------------------------
Q9. Few difference between Var and Dynamic keyword in C#?

Answer:
  1. Var was introduced in C# 3.0 and Dynamic keyword was introduced in C# 4.0
  2. Var is early bounded variable while dynamic is late bounded variable.
  3. With Var variable we have all possible Visual Studio intelli sense but with dynamic keyword we don't have any intelli sense as it a late bounded or run time bounded variable.
  4. Var variable has to be initialized with value.
--------------------------------------------------------------------------------------------------------------------------
Q10. Can we put different types in Dynamic list at run time. example 1st we put customer class and then we put seller class in C#?

Answer: 
Yes we can do. we can dynamic object like 
dynamic d =  new Customer(); and in next line 
d = new Seller();
--------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment