问题
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.
A interface extending another interface.(This is the case with the example in your question.)
A class implementing a interface
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