Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

interface

An interface defines a contract. A class or struct that implements an interface must adhere to its contract. The declaration takes the following form::

[attributes] [modifiers] interface identifier [:base-list] {interface-body}[;]

where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
identifier
The interface name.
base-list (Optional)
A list that contains one or more explicit base interfaces separated by commas.
interface-body
Declarations of the interface members.

Remarks

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. In the following example, the interface IMyInterface inherits from two base interfaces, IBase1 and IBase2:

interface IMyInterface: IBase1, IBase2
{
   void MethodA();
   void MethodB();
}

Interfaces can be implemented by classes and structs. The identifier of the implemented interface appears in the class base list. For example:

class Class1: Iface1, Iface2
{
   // class members
}

When a class base list contains a base class and interfaces, the base class comes first in the list. For example:

class ClassA: BaseClass, Iface1, Iface2 
{
   // class members
}

For more information on interfaces, see Interfaces.

For more information on properties and indexers, see Property Declaration and Indexer Declaration.

Example

The following example demonstrates interface implementation. In this example, the interface IPoint contains the property declaration, which is responsible for setting and getting the values of the fields. The class MyPoint contains the property implementation.

// keyword_interface.cs
// Interface implementation
using System;
interface IPoint 
{
   // Property signatures:
   int x 
   {
      get; 
      set; 
   }

   int y 
   {
      get; 
      set; 
   }
}

class MyPoint : IPoint 
{
   // Fields:
   private int myX;
   private int myY;

   // Constructor:
   public MyPoint(int x, int y) 
   {
      myX = x;
      myY = y;
   }

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

      set 
      {
         myX = value; 
      }
   }

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

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

   public static void Main() 
   {
      MyPoint p = new MyPoint(2,3);
      Console.Write("My Point: ");
      PrintPoint(p);
   }
}

Output

My Point: x=2, y=3

See Also

C# Keywords | Reference Types | Properties | Indexers | C. Grammar | Explicit Interface Implementation Tutorial | class | struct

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft