https://msdn.microsoft.com/en-us/library/ms173156.aspx

An interface contains definitions for a group of related functionalities that a class or a struct can implement.

By using interfaces, you can, for example, include behavior from multiple sources in a class.

That capability is important in C# because the language doesn't support multiple inheritance of classes.

In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.

You define an interface by using the interface keyword, as the following example shows.

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

Any class or struct that implements the IEquatable<T> interface must contain a definition for an Equals method that matches the signature that the interface specifies.

As a result, you can count on依靠 a class that implements IEquatable<T> to contain an Equals method with which an instance of the class can determine whether it's equal to another instance of the same class.

The definition of IEquatable<T> doesn’t provide an implementation for Equals. The interface defines only the signature.

In that way, an interface in C# is similar to an abstract class in which all the methods are abstract.

However, a class or struct can implement multiple interfaces, but a class can inherit only a single class, abstract or not.

Therefore, by using interfaces, you can include behavior from multiple sources in a class.

For more information about abstract classes, see Abstract and Sealed Classes and Class Members.

Interfaces can contain methods, properties, events, indexers, or any combination of those four member types.

For links to examples, see Related Sections.

An interface can't contain constants, fields, operators, instance constructors, destructors, or types.

Interface members are automatically public, and they can't include any access modifiers.

Members also can't be static.

To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

When a class or struct implements an interface, the class or struct must provide an implementation for all of the members that the interface defines.

The interface itself provides no functionality that a class or struct can inherit in the way that it can inherit base class functionality.

However, if a base class implements an interface, any class that's derived from the base class inherits that implementation.

The following example shows an implementation of the IEquatable<T> interface.

The implementing class, Car, must provide an implementation of theEquals method.

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

Properties and indexers of a class can define extra accessors for a property or indexer that's defined in 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, seeExplicit Interface Implementation (C# Programming Guide) and Interface Properties (C# Programming Guide).

Interfaces can implement other interfaces.

A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces implement.

However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (class ClassName : InterfaceName).

If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface.

However, the derived class can reimplement the interface members instead of using the inherited implementation.

A base class can also 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.

Interfaces Summary

An interface has the following properties:

  • An interface is like an abstract base class. Any class or struct that implements the interface must implement all its members.

  • An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.

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

  • Interfaces contain no implementation of methods.

  • A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

In This Section

Explicit Interface Implementation (C# Programming Guide)

Explains how to create a class member that’s 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.

Related Sections

See Also

 

Interfaces (C# Programming Guide)的更多相关文章

  1. Generic Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...

  2. Polymorphism (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173152.aspx Polymorphism is often referred to as the thir ...

  3. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

  4. View Programming Guide for iOS_读书笔记[正在更新……]

    原文:View Programming Guide for iOS 1 Introduction 先熟悉一下基本概念. Window Windows do not have any visible c ...

  5. View Controller Programming Guide for iOS---(二)---View Controller Basics

    View Controller Basics Apps running on iOS–based devices have a limited amount of screen space for d ...

  6. View Controller Programming Guide for iOS---(一)---About View Controllers

    About View Controllers View controllers are a vital link between an app’s data and its visual appear ...

  7. View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations

      Animations Animations provide fluid visual transitions between different states of your user inter ...

  8. View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows

    Windows Every iOS application needs at least one window—an instance of the UIWindow class—and some m ...

  9. View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture

    View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...

随机推荐

  1. C++中的初始化列表

    C++11扩大了列表初始化的适用范围,使其可以用于所有内置类型和用户定义的类型(即类对象). 1.当列表初始化用于内置类型的变量时,这种初始化形式有一个重要的特点:如果我们使用列表初始化且初始化值存在 ...

  2. composer install提示需要输入账号解决方法

    1.问题描述:输入composer install提示需要输入账号,如下所示: 2.解决方法,改用社区的源:composer config -g repo.packagist composer htt ...

  3. day21 04 三级菜单

    day21 04 三级菜单 1.使用递归调用的方法 整体代码类型比较简单如下: menu={'北京':{'海淀':{'a':{},'h':{},'c':{}},'昌平':{'沙河':{},'天通苑': ...

  4. MT4系统自带指标代码

    MT4系统自带指标代码 ~ Accelerator Oscillator 震荡加速指标:                   double iAC() ~ Accumulation/Distribut ...

  5. python 用 PIL 模块 画验证码

    PIL 简单绘画 def get_code_img(request): from PIL import Image, ImageDraw, ImageFont import random def ra ...

  6. linux & chmod & 777

    linux & chmod & 777 https://github.com/xgqfrms-GitHub/Node-CLI-Tools/blob/master/bash-shell- ...

  7. [ZJOI2010] 数字统计

    [ZJOI2010] 数字统计 题目 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. INPUT 输入文件中仅包含一行两个整数a.b,含义如上所述 OUTP ...

  8. codeforces 361B

    #include<stdio.h> int a[100100]; int main() { int n,i,k; while(scanf("%d%d",&n,& ...

  9. restful(1):序列化

    restful协议中,一切皆是资源,操作只是请求方式 model_to_dict()方法: from django.forms.models import model_to_dict obj = Pu ...

  10. ZOJ - 3829 Known Notation(模拟+贪心)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...