C#: Signatures

Adopt from

[1]C# Language Specification Section 3.6

[2]http://msdn.microsoft.com/en-us/library/586y06yf.aspx

[3]http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

[4]http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

[5]http://msdn.microsoft.com/en-us/library/ee332485.aspx

[6]http://msdn.microsoft.com/en-us/library/8edha89s.aspx


Methods, instance constructors, indexers, and operators are characterized by their signatures. All signatures must be unique in the class.

Method Signature[1]

consists of the name of the method, the number of type parameters and the number, modifiers, and types of its parameters. The signature of a method does not include the return type. The signature of a method specifically does not include the return type, nor does it include the params modifier that may be specified for the right-most parameter, nor the optional type parameter constraints..

Example[2]

public static void WriteLine( string format, Object arg0 )

WriteLine is the name of the method;

string and Object are the type of each parameters.

Instance Constructor Signature[1]

consists of the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. The signature of an instance constructor specifically does not include the params modifier that may be specified for the right-most parameter.

Example[3]

static void Method(out String s)

String is the type.

out is part of the signature. IS NOT PRECISE

static void Method(out String s) {}

static void Method(ref String s) {}

will produce a compile time error since out and ref are not part of the signature.[6]

Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:

static void Method(out String s) {}

static void Method(String s) {}

Because of this result, I think out and ref both generate the same signature but with different implementation in CIL.

Indexer Signature[1]

consists of the type of each of its formal parameters, considered in the order left to right. The signature of an indexer specifically does not include the element type.

Example[4]

public T this[int i]

The type is int.

Operator Signature[1]

consists of the name of the operator and the type of each of its formal parameters, considered in the order left to right. The signature of an operator specifically does not include the result type.

Example[5]

public static Complex operator +(Complex c1, Complex c2)

Name is Complex

Type is Complex

Use of Signatures[1]

Signatures are the enabling mechanism for overloading of members in classes, structs, and interfaces:

  • Overloading of methods permits a class, struct, or interface to declare multiple methods with the same name, provided their signatures are unique within that class, struct, or interface.
  • Overloading of instance constructors permits a class or struct to declare multiple instance constructors, provided their signatures are unique within that class or struct.
  • Overloading of indexers permits a class, struct, or interface to declare multiple indexers, provided their signatures are unique within that class, struct, or interface.
  • Overloading of operators permits a class or struct to declare multiple operators with the same name, provided their signatures are unique within that class or struct.
本篇發表於 C#, Programming 並標籤為 , 。將永久鏈結加入書籤。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *