Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Programming Guide
Interfaces (C# Programming Guide)

Interfaces are defined by using the interface keyword, as shown in the following example:

C#
interface IEquatable<T>
{
    bool Equals(T obj);
}

Interfaces describe a group of related functionalities that can belong to any class or struct. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain fields. Interfaces members are automatically public.

When a class or struct is said to inherit an interface, it means that the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation.

Classes and structs can inherit from interfaces in a manner 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 only the method names and signatures, because the interface itself contains no implementations. For example:

    C#
    public class Car : IEquatable<Car>
    {
        public string Make {get; set;}
        public string Model { get; set; }
        public string Year { get; set; }
    
        // Implementation of IEquatable<T> interface
        public bool Equals(Car car)
        {
            if (this.Make == car.Make &&
                this.Model == car.Model &&
                this.Year == car.Year)
            {
                return true;
            }
            else
                return false;
        }
    }
    
    

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 IEquatable<(Of <(T>)>) interface announces to the user of the object that the object can determine whether it is equal to other objects of the same type, and the user of the interface does not have 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 one time, 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. For more information about virtual members, see Polymorphism.

An interface has the following properties:

  • An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all its members.

  • An interface cannot be instantiated directly.

  • Interfaces can contain events, indexers, methods and properties.

  • Interfaces contain no implementation of methods.

  • Classes and structs can inherit from more than one interface.

  • An interface can itself inherit from multiple interfaces.

Explicit Interface Implementation (C# Programming Guide)

Explains how to create a class member that is specific to an interface.

How to: Explicitly Implement Interface Members (C# Programming Guide)

Provides an example of how to explicitly implement members of interfaces.

How to: Explicitly Implement Interface Members with Inheritance (C# Programming Guide)

Provides an example of how to explicitly implement members of interfaces with inheritance.

Interface Properties (C# Programming Guide)

Explains how to declare properties on an interface.

Indexers in Interfaces (C# Programming Guide)

Explains how to declare indexers on an interface.

How to: Implement Interface Events (C# Programming Guide)

Illustrates how an interface can declare an event.

Classes and Structs (C# Programming Guide)

Describes how C# uses objects, classes and structs.

Inheritance (C# Programming Guide)

Explains how C# implements inheritance.

Methods (C# Programming Guide)

Explains the concepts of named methods in the C# programming model.

Polymorphism (C# Programming Guide)

Describes how classes can be used as more than one type in C#.

Abstract and Sealed Classes and Class Members (C# Programming Guide)

Describes how the abstract and sealed keywords affect inheritance.

Properties (C# Programming Guide)

Explains the concept of properties in C#.

Events (C# Programming Guide)

Explains the concept of events in C#.

Indexers (C# Programming Guide)

Explains the concept of indexers in C#.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
declaring and using an interface      Kofi A   |   Edit   |  
To declare and use an interface here are the possible ways:-

1)IInterfaceName i = new ObjectInheritingFromInterface() as IInterfaceName

2) ObjectInheritingFromInterface o= new ObjectInheritingFromInterface();
IInterfaceName i = o as IInterfaceName;
3)IInterfaceName i = (IInterfaceName)(new ObjectInheritingFromInterface)

Tags What's this?: and (x) c# (x) declaring (x) in (x) interface (x) using (x) Add a tag
Flag as ContentBug
Abstract classes inheriting an interface      Brian Arsuaga2   |   Edit   |  
There appears to be some contradictory information in this document.

An interface has the following properties:

An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all its members.

But the c# spec says:


20.4.5 Abstract classes and interfaces

1. Like a non-abstract class, an abstract class must provide implementations
of all members of the interfaces that are listed in the base class list of
the class.

It seems contradictory, but it is not, quite.

The abstract class must provide a prototype for the interface's method, but is not required to provide a body.

So you may:

public interface IExample {

void Action();

}

public abstract class AbsExample : IExample {

abstract void Action(); // this is required for AbsExample to compile

}

Note that if you do it this way you may NOT explicitly implement the interface's methods- probably because a child class cannot override explicitly implemented interface methods. (I think?)

Therefore this does not compile:

public abstract class BrokenExample : IExample {

abstract void IExample.Action();

}

Because abstract is not a valid modifier for the explicitly-implemented method.

It remains unclear to me, however, why the abstract is required to implement the interface at all and cannot simply delegate that responsibility to its eventual descendants. After all, interfaces do this:

public interface IChild : IExample {

void Foo();

}

public class Burdened : IChild {

//Must implement both Action and Foo.

}

-- as do abstract classes that inherit from another abstract class- they pass down the implementation responsibility to the child.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker