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. CF1000G Two-Paths

    题目大意:给你一棵树,其中点上和边上都有值.定义2-Path为经过一条边最多两次的路径,价值为经过点的权值加和-经过边权值*该边经过次数.4e5组询问,每次询问树上连接x,y两点的2-Path的最大价 ...

  2. 笔试算法题(19):判断两条单向链表的公共节点 & 字符集删除函数

    出题:给定两个单向链表的头结点,判断其是否有公共节点并确定第一个公共节点的索引: 分析: 由于是单向链表,所以每个节点有且仅有一个后续节点,所以只可能是Y型交叉(每条链表中的某个节点同时指向一个公共节 ...

  3. IDEA基本使用及配置(1)

    前言:现在IDEA用的人很多,我以前都是用Eclipse的,都说这个IDE比较智能.好用,于是学习一下. IDEA与Eclipse目录结构对比: IDEA中的Project相当于Eclispe中的wo ...

  4. Yahoo前端优化的35条军规

    摘要:无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化34条军规,不过现在已经是35条了,所以可以说是雅虎前端优化的35条军 ...

  5. js 技巧 (二)

    //最小化,最大化,关闭 <object id=min classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">  & ...

  6. mysql基准测试与sysbench工具

    一.基准测试简介  1.什么是基准测试 数据库的基准测试是对数据库的性能指标进行定量的.可复现的.可对比的测试. 基准测试与压力测试 基准测试可以理解为针对系统的一种压力测试.但基准测试不关心业务逻辑 ...

  7. SpringMVC参数接收

    1 绑定简单类型 要根据id查询商品数据,需要从请求的参数中把请求的id取出来.Id应该包含在Request对象中.可以从Request对象中取id. public ModelAndView item ...

  8. windows窗口过程函数名词解析

    windows窗口过程函数名词解析 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) 1. LR ...

  9. Directory获取方式

    1) FSDirectory.open FSDirectory.open()会以最合适的方式来获取一个Directory对象. 2) RAMDirectory 可以将磁盘中的索引加载到内存中,访问速度 ...

  10. 重启系统media服务

    1.adb shell 执行 stop media & start media 或者stop media ; start media 2.代码里执行 import android.os.Sys ...