IT Community - Software Programming, Web Development and Technical Support

Interfaces in C#

This is a discussion on Interfaces in C# within the C# Programming forums, part of the Software Development category; Interfaces are defined using the interface keyword. For example: interface IComparable { int CompareTo(object obj); } Interfaces describe a group of ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-29-2007, 12:09 PM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Default Interfaces in C#

Interfaces are defined using the interface keyword. For example:

Quote:
interface IComparable
{
int CompareTo(object obj);
}
Interfaces describe a group of related behaviors that can belong to any class or struct. Interfaces can be made up of methods, properties, events, indexers, or any combination of those four member types. An interface can not contain fields. Interfaces members are automatically public.

Classes and structs can inherit from interfaces similar to how classes can inherit a base class or struct, with two exceptions:

* A class or struct can inherit more than one interface.
* When a class or struct inherits an interface, it inherits member definitions but not implementations. For example:

Quote:
public class Minivan : Car, IComparable
{
public int CompareTo(object obj)
{
//implementation of CompareTo
return 0; //if the Minivans are equal
}
}
To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers on a class can define extra accessors for a property or indexer defined on an interface. For example, an interface may declare a property with a get accessor, but the class implementing the interface can declare the same property with both a get and set accessor. However, if the property or indexer uses explicit implementation, the accessors must match.

Interfaces and interface members are abstract; interfaces do not provide a default implementation. For more information, see Abstract and Sealed Classes and Class Members.

The IComparable interface announces to the user of the object that the object can compare itself to other objects of the same type, and the user of the interface does not need to know how this is implemented.

Interfaces can inherit other interfaces. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface once, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
interfaces ahmad nadeem Java Programming 2 03-24-2008 07:05 AM
mysql and mysqli interfaces sivakumar Database Support 1 07-20-2007 02:49 AM
Inherit multiple interfaces vigneshgets C# Programming 1 07-12-2007 04:34 AM


All times are GMT -7. The time now is 02:44 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0