What is interface in C# ?


Interface is only the signature of methods, delegates or events. The implementation of methods in done in the class that implement the interface.
Feature of interface
1.It contains the interface Keyword.
2.By default all member of interface will be public
3.We can’t use private or sealed in interface method.
Interface Iclass
{
Private void dowork(); //It will show error.
}
4.To implement interface we have to use colon ( : ) similar to inheritance.
5.Interface also contains the property declarations. The class implement these interface must provide the implementation for these properties.

Syntax of Simple interface in asp.net

Step1: Create one interface in asp.net like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for IClass1
/// </summary>
public interface IClass1
{
string SayHello();
}

Step2:Implement the interface like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1:IClass1
{

public string SayHello()
{
string str="I m from Inferface";
return str;
}
}

Step3: Call the method in asp.net code behind like this

protected void BtnClick_Click(object sender, EventArgs e)
{
Class1 obj = new Class1();
Lblmsg.Text = obj.SayHello();
}

Then you wull get op like this

How to use multiple inheritance using interface
For multiple inheritance we have to create 3 interface, we have already created one interface, now we will create 2 more interface like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for IClass2
/// </summary>
public interface IClass2
{
string SayHello1();

}

Now we will inherit Fist and second interface in third interface like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for IClass3
/// </summary>
public interface IClass3:IClass1,IClass2
{
string SayHello3();

}

Now we implement the method in Multiple class like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for MultipleClass
/// </summary>
public class MultipleClass:IClass3
{

public string SayHello3()
{
string str1 = "I m from third interface";
return str1;
}

public string SayHello()
{
string str1 = "I m from first interface";
return str1;
}

public string SayHello1()
{
string str1 = "I m from Second interface";
return str1;
}
}

Now call the method in asp.net code behind page like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MultipleClass obj = new MultipleClass();
Lblmsg1.Text = obj.SayHello();
}
protected void Button2_Click(object sender, EventArgs e)
{
MultipleClass obj = new MultipleClass();
Lblmsg2.Text = obj.SayHello1();
}
protected void Button3_Click(object sender, EventArgs e)
{
MultipleClass obj = new MultipleClass();
Lblmsg3.Text = obj.SayHello3();

}
}

Now complile code you will get op like this

What is the exact use of interface

1.Since C# does not support multiple inheritance so using interface we can do this task

2.If we have to design some big project and if we have to distribute the work into multiple developer then we can go for this approach.

3.We can use in the data hiding scenario. Suppose if we don’t want to give full access of some class then we can use interface.

4.Interfaces also allow us to program against a contract rather than a concrete implementation. That helps with loose coupling. For more detail on this read this artical
http://fatagnus.com/program-to-an-interface-not-an-implementation/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: