简体   繁体   中英

What does this colon mean in this C# code?

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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