Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between different types of Instantiation in c#

Tags:

c#

.net

I would like to know the difference between these two Instantiation

interface ITest
{
    int TotalMarks(int englishMarks, int mathematicsMarks);
}

class TestClass : ITest
{
    public int TotalMarks(int engMarks, int mathMarks)
    {
        return engMarks + mathMarks;
    }
}

class Program
{
    static void Main(string[] args)
    {
        TestClass c = new TestClass();
        Console.Write(c.TotalMarks(10, 20));
        Console.Write("\n");

        ITest c1 = new TestClass();
        Console.Write(c1.TotalMarks(21, 34));

        Console.ReadKey();
    }
}
  • TestClass c = new TestClass();
  • ITest c1 = new TestClass();

    they both work and give result as expected. How these two differ and when to use which ?

like image 564
NoobieDude Avatar asked Dec 25 '22 08:12

NoobieDude


2 Answers

The difference is that on second one using an interface you can only access those members which are present on that specific interface instead the whole class. This allows you to implement a few different interfaces on your actual class, whereas only a specific "contrainer" is accessable by the user.

Furthermore interface-driven design is good on unit-testing as you can simply exchange one class by another.

Imagine you have another class that also implements your interface. If a method you created in any context would expect you actual class as parameter you would now have to change that method´s signature in order to also allow instances of your second class. If the method is designed for interfaces instead you can pass both - TestClass and AnotherTestClass-instances to it without taking care what type it actual is. This reduces class-coupling as you´re no longer depending on an actual class but only the members an interface defines. How those members are implemented does not have any meaning to your consuming code.

Also take care when casting. Whilst you can say that every instance of TestClass also implements ITest not all instances of ITest are of type TestClass. Thus the following produces an InvalidCastException at runtime:

ITest t = new TesClass();
AnotherTestClass t2 = (AnotherTestClass) t;

All in all an interface only says what an instance of that interface can do, but not how it achieves this as this should be unimportant for any consuming code. To stay on your example: your Program won´t need to know how TotalMarks is actually implemented as long as it knows that the method actually exists and returns what it is supposed to return. The implementation-details are of no meaning the Program. This is what is called losly class-coupling.

like image 114
MakePeaceGreatAgain Avatar answered Feb 12 '23 13:02

MakePeaceGreatAgain


There could be many difference but few of major I would mention

ITest c1 = new TestClass();

Using this Interface to create object allows to create object of any class that implements the ITest like TestClass

class AnotherTestClass : ITest
{
    public int TotalMarks(int engMarks, int mathMarks)
    {
        return engMarks + mathMarks;
    }
}

 ITest c1 = new AnotherTestClass();

On the other hand declare some new method in TestClass and try to access it with c1 created through interface you wont be able to access it but you can access the new method if you create through class in the first method through the class instead of interface as shown below.

class TestClass : ITest
{
    public int TotalMarks(int engMarks, int mathMarks)
    {
        return engMarks + mathMarks;
    }
    public void AdditionalMethod()
    {

    }
}

TestClass c = new TestClass();
c.AdditionalMethod(); //Valid
ITest c1 = new TestClass();
c1.AdditionalMethod(); //Invalid, compilation error

Interfaces help us in creating loosely coupled applications by acting upon pattern program to interface not an implementation, you can read more about it here and here.

like image 40
Adil Avatar answered Feb 12 '23 12:02

Adil