An interface
looks like a class which contains only the declaration of methods, properties,
events or indexers and has no implementation for any member.
Class or
structure that implements interface must implement all interface members.
An interface
can be member of class and namespace and interface has below members:
a. Methods
b. Properties
c. Indexers
d. Events
Points To Remember:
1. Interface
members cannot have any access modifiers even public, default access specifiers
is public.
2. An
interface can extend another interface.
3. Interface
has no implementation for any member.
4. Interface
contains only Methods, Properties, Indexers, delegates and events signature.
5. A class
or structure can implement more than one interface.
6. Interface
cannot be instantiated but it can be referenced by the class object which
implements it.
i.e ICollage collageInfo=new student();
7. In C# multiple inheritance is possible with interface, an interface can inherit from one or more base interfaces.
Interface Example:
public interface
ICollage
{
// Property declaration:
string
Name
{
get;
set;
}
void GetCollageInfo();
}
Implementation:
public class
student : ICollage
{
public
static
int
numberOfStudents;
private
string
name;
public
string
Name //
read-write instance property
{
get
{
return
name;
}
set
{
name = value;
}
}
private
int
counter;
public
int
Counter // read-only instance property
{
get
{
return
counter;
}
}
public
student() // constructor
{
counter = ++counter + numberOfStudents;
}
public
void
GetCollageInfo()
{
Console.Write("Engineering collage");
}
}
Multiple inheritances Example with interface:
public interface
ICollage
{
void GetCollageInfo();
}
public partial
class
Teacher
{
public
static
int
numberOfTeachers;
private
int
counter;
public
int
Counter // read-only instance property
{
get
{
return
counter;
}
}
public
Teacher() // constructor
{
counter = ++counter +
numberOfTeachers;
}
}
public class
student :Teacher,ICollage
{
public
void
DiaplyStudentData()
{
Console.WriteLine("i am Nagnath Kendre");
GetCollageInfo();
}
public
void
GetCollageInfo()
{
Console.Write("Engineering collage");
}
static
void
main()
{
student
collageInfo = new student();
Teacher
techerDetails =new Teacher();
collageInfo.DiaplyStudentData();
}
}