C# Language Reference
interface (C# Reference)

Updated: October 2008

An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. In the following example, class ImplementationClass must implement a method named SampleMethod that has no parameters and returns void.

Example

C#
interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation: 
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

An interface can be a member of a namespace or a class and can contain signatures of the following members:

An interface can inherit from one or more base interfaces.

When a base type list contains a base class and interfaces, the base class must come first in the list.

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

For more details and code examples on explicit interface implementation, see Explicit Interface Implementation (C# Programming Guide).

The following example demonstrates interface implementation. In this example, the interface contains the property declaration and the class contains the implementation. Any instance of a class that implements IPoint has integer properties x and y.

C#
interface IPoint
{
   // Property signatures:
   int x
   {
      get;
      set;
   }

   int y
   {
      get;
      set;
   }
}

class Point : IPoint
{
   // Fields:
   private int _x;
   private int _y;

   // Constructor:
   public Point(int x, int y)
   {
      _x = x;
      _y = y;
   }

   // Property implementation:
   public int x
   {
      get
      {
         return _x;
      }

      set
      {
         _x = value;
      }
   }

   public int y
   {
      get
      {
         return _y;
      }
      set
      {
         _y = value;
      }
   }
}

class MainClass
{
   static void PrintPoint(IPoint p)
   {
      Console.WriteLine("x={0}, y={1}", p.x, p.y);
   }

   static void Main()
   {
      Point p = new Point(2, 3);
      Console.Write("My Point: ");
      PrintPoint(p);
   }
}
// Output: My Point: x=2, y=3

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.9 Interfaces

  • 3.4.5 Interface members

  • 4.2.4 Interface types

  • 10.1.2.2 Interface implementations

  • 11.2 Struct interfaces

  • 13 Interfaces

See Also

Concepts

Reference

Other Resources

Change History

Date

History

Reason

October 2008

Fixed typo.

Customer feedback.

Tags :


Community Content

atarikg
Then Why ?
Then why and where would we want to use Interfaces ? Where is the real world problem which can be solved by using Interface ?

Please Microsoft, when writing something, make it more clear and comprehensible. That's what we users want from you.
Tags :

vbscript2
Because you're reading the wrong article.
This article was designed to document the implementation of interfaces in the C# programming language, not to teach you what an interface is or how and where they should be used. They did, however, provide this article http://msdn.microsoft.com/en-us/library/ms173156(VS.80).aspx for the latter purpose. I would particularly recommend reading the "Polymorphism" link off of that article, as you can't really understand the use of interfaces without understanding polymorphism. Actually, I would recommend taking at least the first few introductory computer sciences classes at a local college if you really want to learn that sort of thing. MS' documentation should be doing exactly what it is doing: documenting the language or API, not teaching you how to program. For the latter purpose, you will be much better served by taking Computer Science courses than just by reading MS documentation.
Tags :

Wilee_6
Modifier accessible
(modifier accessible) class Point : IPoint
{
}
hi i,s it in order to implement that interface to the class, can we put an modifier accessible (public) or just force to private only? I try to put public accessible , the complier complain to me
Error 1 Inconsistent accessibility: parameter type 'interfaceTest.IPrey' is less accessible than method 'interfaceTest.Cat.Attack(interfaceTest.IPrey)' D:\Profiles\FMX437\My Documents\Visual Studio 2008\Projects\interfaceTest1\interfaceTest1\Program.cs 50 21 interfaceTest1

, can you explain more regarding for this issue?
Tags :

Page view tracker