Thursday, 5 September 2013

What Is an Interface in C# .Net

In C#, you cannot create a class that inherits from more than one class. However, sometimes it is required to create a class that inherits features from more than one class. This type of inheritance is known as multiple inheritances. C# does not support the feature of multiple inheritance for classes; however it is allow you to take the advantage of the multiple inheritance feature by supporting the concept of interface

An Interface is a collection of abstract data members. An interface contains only the signatures of methods, properties, events or indexers, delegates means an interface looks like a class but has got no implementation. In an interface we cannot do any implementation but we can declare signatures of properties, methods, delegates and events. Implementation part is been handle by the class that implements the interface

The following are some important characteristics of interface

·         An Interface declared by using the interface keyword
·         You cannot create the object of Interface
·         If a class implements an interface ,then it must provide implementations for all the methods of the interface
·         By default interface is  public
·         There is no any field using in interface like int a=10;
·         static keyword is not  allow in interface

General Syntax for Interface

[access-modifier] interface <Interface Name>
{
//method declarations
}

In the preceding syntax:
·         Access-modifier : Specifies the scope in which the interface can be accessed
·         Interface : Represents a keyword that declares an interface
·         Interface Name: specified name for interface

For Example:

public interface MyInterface
{
void displaymarks(int m);
}

class that implements the interface

public class MyClass:MyInterface
{
private int marks;
public void displaymarks(int m)
{
marks = m;
MessageBox.Show("marks" + marks);
}

}

0 comments:

Post a Comment