Sunday, June 8, 2008

C# Interface

An interface is a class with only signatures of methods properties and events without any implementation.
It is up to implementing class to add functionality . Your can't create an instance of interface.
Interface is an contract between an interface and implementing class that the implementing class should implements all the members of the interface.
It is possible to have multiple interface inheritance.

Start a new Windows Forms application or Wpf Application.

Add a new Interface by selecting Project, Add New Item and Interface from the Add NewItem Dialog.

interface ISpecialization
string Specialization { get; set; }

Likewise create another Interface called IHobby.

interface IHobby
string Hobby { get; set; }

Now create a class which implements both ISpecailization and IHobby.

class Person : ISpecialization, IHobby
public string Hobby { get; set; }
public string Specialization { get; set; }

Now in Wpf Form you can instantiate Person class and use it.

Person Shalvin;
private void Window_Loaded(object sender, RoutedEventArgs e)
Shalvin = new Person();
Shalvin.Specialization = ".Net";
Shalvin.Hobby = "Blogging";

private void btnHobby_Click(object sender, RoutedEventArgs e)

private void btnSpecialization_Click(object sender, RoutedEventArgs e)

Though we are using inheritance operator (:) behind the scene it is doing an implementation as is evident form the IL code below.

If your are not conversant with Intermediate Language visi my blog :

No comments:

Post a Comment