开发者

What does this colon mean in this C# code?

开发者 https://www.devze.com 2023-04-11 07:52 出处:网络
What does the : 开发者_JS百科indicates in the class or interface definition in C#. public interface IServer : IServerManager, ISimulation, ISiteEx

What does the : 开发者_JS百科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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号