What does this colon mean in this C# code?

做~自己de王妃 提交于 2021-02-18 22:24:32

问题


What does the : indicates in the class or interface definition in 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; }
}

回答1:


: 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.




回答2:


Because IServer is an interface, the colon means that the IServer interface inherits from the IServerManager, ISimulation, ISiteEx interfaces. In other words: any class or struct that implements IServer must also implement the other three.

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.




回答3:


It means the interface is implementing another interface, or number of interfaces.




回答4:


: is the way to implement inheritance in c# There are multiple scenarios that can use it.

  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.



来源:https://stackoverflow.com/questions/7656522/what-does-this-colon-mean-in-this-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!