Wednesday 15 April 2015

What is interfaces in c# with example:-


This blog we will discuss what is interface and what is the use in c#.
 An interface in c# is a reference type. Actually C# does not support multiple inheritance.  C# provides alternative approach known as interface to support the concept of multiple inheritance.
Limitations of interface:-
1:- All the members of an interface are implicitly public and abstract.
2:-An interface cannot contain     constant fields, constructors and destructors.
3:-Its members cannot be declared static.
4:-An interface can inherit multiple interfaces.
5:- An interface can contain one or more methods, properties, indexers and events
Example;-
interface ISimpleinter
{
    void SampleMethod();
}

class ImplementationClass : ISampleInter
{
    // 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();
    }
}



No comments:

Post a Comment