Qual è la differenza tra l'intestazione del metodo e la firma del metodo?

6

Voglio sapere qual è esattamente l'intestazione di un metodo e qual è la firma di un metodo ed è la stessa tra i linguaggi di programmazione o solo in C #?

Quindi, è corretto affermare quanto segue è un'intestazione di metodo:

public void SumNumbers(int firstNumber, int secondNumber)

e la seguente è la firma del metodo:

public void SumNumbers(int, int)

    
posta user109547 22.11.2013 - 23:57
fonte

1 risposta

6

In base alla specifica C # , un L'intestazione del metodo è composta da:

attributesopt method-modifiersopt return-type member-name (formal-parameter-listopt)

Quindi estendi ciò che hai mostrato:

[OperationContract]
public void SumNumbers(int firstNumber, int secondNumber)

L'esempio sopra contiene esempi delle parti possibili che costituiscono un'intestazione del metodo, mentre il minimo (tutte le parti non facoltative) sarebbe qualcosa del tipo:

void Foo()

Per quanto riguarda la firma, vedi 3.6 Firme e sovraccarico :

The signature of a method consists of the name of the method and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. 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.

The following example shows a set of overloaded method declarations along with their signatures.

interface ITest
{
   void F();                  // F()
   void F(int x);             // F(int)
   void F(ref int x);         // F(ref int)
   void F(int x, int y);      // F(int, int)
   int F(string s);           // F(string)
   int F(int x);              // F(int)         error, F(int) already exists
   void F(string[] a);        // F(string[])
   void F(params string[] a); // F(string[])      error, F(string[]) already exists
}

Quindi la firma del tuo secondo esempio sarebbe:

SumNumbers(int, int)

Vedi anche Method Signature in C # su SO, in particolare la risposta di Eric Lippert.

    
risposta data 23.11.2013 - 00:52
fonte

Leggi altre domande sui tag