6

I want to know what is exactly a method header and what is a method signature and is this the same among programming languages or just in C#?

So, is it correct to say the following is a method header:

public void SumNumbers(int firstNumber, int secondNumber)

and the following is the method signature:

public void SumNumbers(int, int)

?

  • Definitions of "signature" vary across languages. – Ross Patterson Nov 23 '13 at 14:02
6

According to the C# spec, a method header consists of:

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

So to extend what you've shown:

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

Above example contains examples of the possible parts that make up a method header, while the minimum (all non-optional parts) would be something like:

void Foo()

As for the signature, see 3.6 Signatures and overloading:

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
}

So the signature of your second example would be:

SumNumbers(int, int)

See also Method Signature in C# on SO, especially Eric Lippert's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.