C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

interface


Posted Date: 26 Sep 2008      Posted By: subhashini      Member Level: Bronze     Points: 1   Responses: 2



what is an interface?


thank you





Responses

Author: vasanthiraajan    26 Sep 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

hi,

Refer this,

1.An Interface is a reference type and it contains only abstract members.
2.Interface's members can be Events, Methods, Properties and Indexers.
3. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them.
4.The interface can't contain constants, data fields, constructors, destructors and static members.
5.All the member declarations inside interface are implicitly public.

Defining an Interface:
Let us look at a simple example for c# interfaces. In this example interface declares base functionality of node object.

E.g:

interface INode
{

string Text
{
get;
set;
}

object Tag
{
get;
set;
}

int Height
{
get;
set;
}

int Width
{
get;
set;
}

float CalculateArea();

}


The above INode interface has declared a few abstract properties and function which should be implemented by the derived classes.

//Sample for Deriving a class using a C# .Net interface - c# tutorial

public class Node : INode
{

public Node()
{}
public string Text
{

get
{
return m_text;
}

set
{
m_text = value;
}

}

private string m_text;
public object Tag
{

get
{
return m_tag;
}

set
{
m_tag = value;
}

}

private object m_tag = null;
public int Height
{

get
{
return m_height;
}

set
{
m_height = value;
}

}

private int m_height = 0;
public int Width
{

get
{
return m_width;
}

set
{
m_width = value;
}

}

private int m_width = 0;

public float CalculateArea()
{

if((m_width<0)||(m_height<0))
return 0;

return m_height*m_width;

}

}


Now the above code has created a c# class Node that inherits from INode c# interface and implement all its members. A very important point to be remembered about c# interfaces is, if some interface is inherited, the program must implement all its declared members. Otherwise the c# compiler throws an error.

The above code was a simple example of c# interface usage. Now this has to be followed with some advanced details of interface building in C# .Net. The previous example used only names of methods or properties that have the same names as in interface. But there is another alternative method for writing the implementation for the members in class. It uses full method or property name e.g. INode.CalculateArea () {// implemetation}.

Multiple Inheritance using C# interfaces:

Next feature that obviously needs to be explained is multiple inheritance using c# interfaces. This can be done using child class that inherits from any number of c# interfaces. The inheritance can also happen with a combination of a C# .Net class and c# interfaces. Now let us see a small piece of code that demonstrate us multiple inheritance using only interfaces as parent data types.

class ClonableNode : INode,ICloneable
{

public object Clone()
{
return null;
}

// INode members
}


The above example created a class ClonableNode. It implements all the functionality of INode interface in the same way as it was done in Node class. Also it realizes Clone method only one item of IClonable interface of .NET library.



Author: Sidewinder2    26 Sep 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 4

interface are abstract classes which containg the signature of method,
variables etc.

we can use the interface in any class by implementing it as

public class :Interface1
{
....some code
}

this reduces the time for defining similar method or variables in each and every class

just go to www.Microsoft.com > for developers > msdn > library >Visual C#


cheers!,
Myself



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : retreiving data into winform textboxes during form load
Previous : How to set DataGird focus in C#.net Windows App
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use