1

can you please explain why really i need use interface instead of directly declaring and implementing method inside of Class. Above i have two example. Class with interface and without. Thanks in advance.

EXAMPLE1 Why i should use this

interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation: 
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

EXAMPLE2 why i shouldn't use this

class ImplementationClass
    {
        void SampleMethod()
        {
            // Method implementation.
        }

        static void Main()
        {
            // Declare an interface instance.
            ImplementationClass obj = new ImplementationClass();

            // Call the member.
            obj.SampleMethod();
        }
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • There's no reason why you shouldn't use the second one, what is your actual use case? – Lee Nov 20 '17 at 20:05
  • I would actually do a google search on the difference between `Class vs Interfaces` this would give you a better understanding on the `When and Why` – MethodMan Nov 20 '17 at 20:07

0 Answers0