21 out of 31 rated this helpful - Rate this topic

Interfaces (C# Programming Guide)

Updated: July 2010

Interfaces describe a group of related functionalities that can belong to any class or struct. You define an interface by using the interface keyword, as shown in the following example.


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


Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.

When a class or struct implements an interface, 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. The derived class is said to implement the interface implicitly.

Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions:

  • A class or struct can implement more than one interface.

  • When a class or struct implements an interface, it receives only the method names and signatures, because the interface itself contains no implementations, as shown in the following example.

    
    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;
        }
    }
    
    
    

The IEquatable<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.

To implement an interface member, the corresponding member of the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers of a class can define extra accessors for a property or indexer defined on an interface. For example, an interface might declare a property that has a get accessor. The class that implements 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. For more information about explicit implementation, see Interface Properties (C# Programming Guide)

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

Interfaces can inherit other interfaces. It is possible for a class to inherit an interface multiple times, through base classes it inherits or through interfaces inherited by other interfaces. However, the class can implement an interface only one time, and only if the interface is declared as part of the definition of the class, as in class ClassName : InterfaceName. If the interface is inherited because you inherited a base class that implements the interface, its implementation is provided by the base class. It is also possible for a base class to implement interface members by using virtual members. In that case, a derived class 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 that implements 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 implement more than one interface.

  • An interface itself can 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 Members of Two Interfaces (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#.

Date

History

Reason

July 2010

Distinguished between implementation and inheritance. Clarified what can and cannot be included in interfaces.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
C# Interfaces
C# Interfaces : http://planetofcoders.com/c-interfaces/
RE: Interfaces no static methods or properties - Why?

Hi  RichardUK ,

What you are describing is a difference between definition and use.
An interface is defined as "a group of related functionalities that can belong to any class or struct." and is used to refer to instances of different object types which have a similar form.  For example, you might have an app which allows users to sort large amounts of data.  Because the user may have additional information on the general format of the data (partially sorted, not sorted, random, sorted, sorted in reverse order, etc.) you decided to add the ability for the user to specify which algorithm will be used to preform the sort.  Rather then write code for each class, you define an interface and use the interface to access the sort, get, and find methods.
Interfaces are often used for a different reason.  In business there are often many people working on the same project.  To prevent problems, managers will often define interfaces which will act as a contract between the developers.

The situation you are describing seems to be in the latter category, where you want to define a static method in the interface.  This of course does not make sense, as an interface must point to an instance of a class or struct.  Can you call a static method from an instance of a class or struct?  No, you have to call it from the class it self.

If you want to "tell the implementer what he/she should implement in their concrete class." I would suggest an abstract class.  An Abstract class will allow you to define the structure of the class, while leaving the implementation in the hands of someone else. 

See Abstract and Sealed Classes and Class Members (C# Programming Guide) http://msdn.microsoft.com/en-us/library/ms173150.aspx

BASE CLASSES TRUMP INTERFACES

Interfaces are for behaviours that cut across class hierarchies. An interface and a class both describe contracts. Classes should be favoured. You do not need an interface for loose coupling, IoC, mocking, or SOA. Use base classes.

http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80%94Choosing_Between_Class_and_Interface

All your colleagues will probably use lots of interfaces for loose-coupling (laughably, usually within the same assembly). They are wrong and following the herd. Even speakers at conferences, including Microsoft's own employess, and people you think are smart, people writing books, are incorrectly using interfaces.

If you have an interface applied to only one type - its surely pointless and will lead to diverging contracts.

http://stackoverflow.com/questions/90851/is-it-just-me-or-are-interfaces-overused

Interfaces no static methods or properties - Why?
An interface is a contract between the creator of the interface and the implementer of the class that implements the interface, part of its aim is to tell the implementer what he/she should implement in their concrete class.

So why oh why are no static methods or properties allowed? They are both public and a legitimate elements of the classes that use them and can form part of the functionality of concrete classes. An interface should be a document that defines what a concrete class should do, part of what concrete classes can do involves static methods and properties - so why exclude them?
Advertisement