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();
}
}