简体   繁体   English

这个冒号在这个C#代码中意味着什么?

[英]What does this colon mean in this C# code?

What does the : indicates in the class or interface definition in C#. 在C#中的类或接口定义中指示了什么:

public interface IServer : IServerManager, ISimulation, ISiteEx
{
    /// <summary>
    /// Returns the highest game version that supported by this server.
    /// Higher versions aren't guaranteed to work perfect.
    /// </summary>
    Version MaxSupportedGameVersion { get; }

    /// <summary>
    /// Gets/sets the current server configuration.
    /// </summary>
    ServerConfiguration Configuration { get; set; }
}

: is used to indicate that the interface on the left side of the operator is implementing ( technically, the classes implementing the interface will give the implementation) the interfaces on the right. :用于指示运算符左侧的接口正在实现(技术上,实现接口的类将给出实现)右侧的接口。

: is used in the same way to indicate when a class is implementing one or more interfaces as well. :以相同的方式用于指示类何时实现一个或多个接口。

Because IServer is an interface, the colon means that the IServer interface inherits from the IServerManager , ISimulation , ISiteEx interfaces. 因为IServer是一个接口,冒号意味着IServer接口继承了IServerManagerISimulationISiteEx接口。 In other words: any class or struct that implements IServer must also implement the other three. 换句话说:任何实现IServer类或结构也必须实现其他三个。

If the type to the left of the colon was a class or struct, the colon would indicate that the class or struct implements the interfaces. 如果冒号左侧的类型是类或结构,冒号将指示类或结构实现接口。 Also in this case, if one (and only one) of the types on the right was a class, it would mean that the type on the left inherits from this class. 同样在这种情况下,如果右侧的一个(并且只有一个)类型是一个类,则意味着左侧的类型继承自该类。 Classes can inherit from many interfaces, but from only one class. 类可以从许多接口继承,但只能从一个类继承。

这意味着接口正在实现另一个接口或多个接口。

: is the way to implement inheritance in c# There are multiple scenarios that can use it. :是在c#中实现继承的方法有多种方案可以使用它。

  1. A interface extending another interface.(This is the case with the example in your question.) 扩展另一个接口的接口(问题中的示例就是这种情况。)

  2. A class implementing a interface 实现接口的类

  3. A class extending another class 扩展另一个类的类

A class can implement multiple interfaces but it can extend only one class. 一个类可以实现多个接口,但它只能扩展一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM