Friday, February 17, 2012

An Interface is a Value type or reference type?

Select from following answers:
  1. Value Type
  2. Reference Type
  3. Vaule type or reference type
  4. both

Because , Interface is a Contract, When we design Interface and implement it in other object , that object will implement this. So, when a struct implement an interface ,that time it is value type and in case of class it is reference type.

1 comment:

  1. How to achieve Multiple Inheritance in C#

    Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, Will a Class fully represent an object? Meaning that, if we have a base class with abstract methods designed for a particular application and we know that the inheriting object will only need the methods defined in that class. We now have a valid design pattern here.

    The Vehicle Car Object

    Lets say we have an abstract class called "Vehicle" as well as another class called "ConstructionVehicle". The vehicle class has methods such as Accelerate() , Stop(), and the "ConstructionVehicle" class has methods such as ExecuteDump() and TurnOnBackUpSound(). If we were only going to build a Car object and know we would only use those methods from the "Automobile" class this would be fine.

    The DumpTruck Object

    Now we want to create another object called "DumpTruck". We could inherit from the Automobile class but that class does not have the methods that we need called ExecuteDump() and TurnOnBackUpSound(). If we were using a language such as C++ we could easily inherit from both classes using multiple inheritance. However, seeing C# is our language of choice, multiple inheritance is not an option, you may only inherit from one Base Class.

    From Abstract Classes to Interfaces

    From a design perspective we must choose a different design. C# supports what is called "Multiple Implementation", which is to says a class can implement more than one interface. Our design now changes the "Vehicle" class and the "ConstructionVehicle" class into interfaces. Below we have defined the two interfaces with their very simplistic methods. i.e :

    interface IConstructionVehicle
    void ExecuteDump();
    void TurnOnBackUpSound();
    interface IVehicle
    void Accelerate();
    void Stop();
    void TurnOnBackUpSound();

    If we built a class that inherited from these two interfaces we would be able to do so spanning multiple inherited interfaces. Design problem solved!

    Or is it?

    Explicit Interface Implementation

    If you look at both interfaces defined above you'll notice that they share in common a method of the same name "TurnOnBackUpSound()". Problem? No, in fact C# supports what is known as "Explicit Interface Implementation", which allows the programmer to specify which member of which interface they want to use. Putting the Interface name in front of the member name allows this to happen as shown below.

    public class DumpTruck: IEngine, IBody
    void IEngine.Test()
    Console.WriteLine("This is the Engine TEst");
    void IBody.Test()
    Console.WriteLine("This is the Body TEst");

    Implementation Hiding

    Another benefit to this technique is something called "Implementation Hiding". Implementation Hiding allows the methods from the implemented interface to be hidden from the derived class unless the developer explicitly calls the interface. This technique obviously reduces the clutter for a developer.