In .NET how to achieve Multiple Inheritance using Interface

Diamond Problem :

The Diamond problem is related to object oriented languages that allow multiple inheritance.As we know C# does not support multiple inheritance of classes, but it does support multiple inheritance through interfaces. Therefore, the same ambiguity may arise unless explicit interface implementation is used. Going explicitly, we can have different implementations to the same method. The problem is solved.




namespace
MulipleInheritance
{


public interface IntBC
{
void Display();
}


public interface IntA : IntBC
{
new void Display();
}


public interface IntB : IntBC
{
new void Display();
}


// C class implements the IntA and IntB interfaces
public class C : IntA, IntB
{
// explicitly implement the Display() method of the IntA interface
void IntA.Display()
{
Console.WriteLine("IntA implementation of Display()");
}
// implement the Display() method of the IntB interface
public void Display()
{
Console.WriteLine("IntB implementation of Display()");
}
}


public class
MainClass
{
public static void Main()
{
C ObjC = new C();
Console.WriteLine("Calling ObjC.Display()");
ObjC.Display();
// cast ObjC to IntB
IntA RefIntA = ObjC as IntA;
Console.WriteLine("Calling RefIntA.Display()");
RefIntA.Display();
// cast ObjC to IntA
IntB RefIntB = ObjC as IntB;
Console.WriteLine("Calling RefIntB.Display()");
RefIntB.Display();
}
}


}

Output :

Calling ObjC.Display()
IntB implementation of Display()
Calling RefIntA.Display()
IntA implementation of Display()
Calling RefIntB.Display()
IntB implementation of Display()

In Class C, first Display() method is explicitly implemented for IntA Interface. Explicit implementation is not needed for the second Display() method, because previous statement already did that and the method implementation is automatically assumed to IntB interface. In order to call explicit implementation method casting is required.



Why Multiple inheritance is not supported in .NET

Multiple Inheritance:

Multiple inheritance may allow you to write compact code but it has some program difficulties like "diamond problem" for example

Diamond problem :



class
BaseClass
{
public abstract void Display();
}

class A : BaseClass
{
public override void Display()
{
Debug.WriteLine("First Child A");
}
}

class B : BaseClass
{
public override void Display()
{
Debug.WriteLine("Second Child B");
}
}

//Now Suppose that c# support multiple inheritance

class C : A, B
{
}

public class MainClass
{
public static void main()
{
C ObjC =new C();
ObjC.Display();
}
}

By excuting ObjC.Display() method Diamond problem is generated. It means which Display method going to executed.

Exception Handling in SQL Server 2005


Exception Handling in 2000:

In SQL Server 2000 Exception handling was implmented with the help of @@ERROR statement. Each and every statement you have to check the value of Error number. Practically it is not easiest things to do.

Exception Handling in 2005:

In .NET programming exception are handled with help of TRY - CATCH block. Similarly TRY - CATCH block is implmented in SQL Server 2005. When you are exceuting the procedure if error occured then catch block statements are exceuted. This technique is applicable only for the run time errors.

The following functions are used to get the error details

ERROR_NUMBER()
ERROR_SEVERITY()
ERROR_STATE()
ERROR_PROCEDURE()
ERROR_LINE()
ERROR_MESSAGE()

Example:

CREATE PROCEDURE PROCEXCEPHANDLING
AS
BEGIN

BEGIN TRY
SELECT 100/0
END TRY

BEGIN CATCH
Select
ERROR_NUMBER() AS ErrNo,
ERROR_SEVERITY() AS ErrSev,
ERROR_STATE() AS ErrState,
ERROR_PROCEDURE() AS ErrProc,
ERROR_LINE() AS ErrLine,
ERROR_MESSAGE() AS ErrMsg
END CATCH

END

Executing the above procedure
Exec PROCEXCEPHANDLING

Output of above statement is


Static Constructor in C#


Static Constructors:


It is a Special type of constructor. It gets called before the creation of first object of class(Probably at the time of loading an assembly).


Rules to create a static constructor:

  • There is no access modifier require to define a static constructor.
  • There may be only one static constructor in a class.
  • The static constructor may not have any parameters.
  • This constructor may only access the static members of the class

Static constructor is used to initialize static data members as soon as the class is referenced first time.You can also initialize static data members where we declare them in the code.

Like this :
public static int id = 10;

But value of one static member may depend upon the value of another static member .In order to handle such cases you need some manipulation before static members going to be initialize.
This you can achieve through static constructors.


namespace StaticConstructor
{


class MainClass
{
static void Main(string[] args)
{
Console.WriteLine("Id value is " + ClsStaticConstructor.id);
}
}


class
ClsStaticConstructor
{
public static int id;

static ClsStaticConstructor()
{
if (Sample.Value == 10)
id = 20;
else
id = 40;
}
}


public class
Sample
{
public static int Value = 10;
}


}


Output
:

Id value is : 20