What is the signature of a method?

40

A signature of the common method / function or constructor is made up of its name + number of parameters + type of parameters?

    
asked by anonymous 10.11.2014 / 11:34

3 answers

32

Signing is the way to uniquely identify a method. In languages where multiple methods can have the same name, you need to have another way to avoid ambiguity. The compiler needs to know which of the methods with the same name you are calling. So you need to avail of the extra information available in the method to make a decision. The most common is to analyze the parameters. If all the parameters are the same, you have the same method, if only one of these parameters is different, you have a different method. It is possible that feedback and other information can be analyzed as well, but this is not common as they can bring some problems. Also understand that order is also important.

Java is one of the languages that went by the safe way of parsing only the parameters. This is often the case. C # was also down this path. C ++ has chosen to use the return type also in some situations.

In theory it would be possible to use any available information up to the name of the parameter. In general this would be a bad idea, but nothing prevents a language from using it. It is she who will determine what is good for her. Java and C # probably took advantage of problems encountered in using C ++ to avoid such flexibility.

Let's look at examples in Java (almost all work fine in C # too):

int FazAlgumaCoisa() { // faz alguma coisa aqui }

int FazAlgumacoisa(int valor) { // faz alguma coisa aqui }

These methods have the same name but are different, they probably perform something (at least) slightly different (although this is not mandatory, it would just be weird to do exactly the same), but not very different, after all, if this happened , would not justify the same name. There is a reasonable chance of the first just calling the second passing a default parameter (but it does not have to be this way). Ex.:

int FazAlgumaCoisa() { FazAlgumaCoisa( 0 ); }

And now you see these methods:

int FazAlgumaCoisa(int valor1) { // faz alguma coisa aqui }

int FazAlgumaCoisa(int valor2) { // faz alguma coisa aqui }

Do they have the same signature or not?

Own it! You would have a conflict if you declared both. The analysis only considers the type of the parameters, not their name.

But I just put the methods declaration, did not show the class declaration. I have found that these methods are of the same class. This means that you may have methods that appear to have the same signature, but in the background the signature is different. See:

int FazAlgumaCoisa() { // faz alguma coisa aqui }

int FazAlgumaCoisa() { // faz alguma coisa aqui }

Is this possible? Well, it's if the methods belong to different classes:

class Exemplo1 {
    int FazAlgumaCoisa() { // faz alguma coisa aqui }
}
class Exemplo2 {
    int FazAlgumaCoisa() { // faz alguma coisa aqui }
}

This is valid. What most people do not understand is that instance methods have an implicit (hidden) parameter. We can call it this . In the background, underneath the cloths, these classes are mounted like this:

class Exemplo1 {
    int FazAlgumaCoisa(Exemplo1 this) { // faz alguma coisa aqui }
}
class Exemplo2 {
    int FazAlgumaCoisa(Exemplo2 this) { // faz alguma coisa aqui }
}

Did you notice that the signature is different? If we used the method that had int as an explicit parameter it would look like this:

class Exemplo1 {
    int FazAlgumaCoisa(Exemplo1 this, int valor) { // faz alguma coisa aqui }
}
class Exemplo2 {
    int FazAlgumaCoisa(Exemplo2 this, int valor) { // faz alguma coisa aqui }
}

Some people will talk about the number of method parameters. But it is obvious that if the number of parameters is different the types are different, after all all types are taken into consideration, so at least you would be comparing a given type with nothing, which is clearly something different. It is obvious that xxx(int, int) is different from xxx(int, int, int) .

Remembering that string, int is different from int, string . The order has relevance.

Other elements can make a difference in the signature depending on the language. See more details on C # in dcastro's answer.

See Java language specification (the original question was in Java).

The most current C # specification can be found for download or direct access (older version). In her answer the dcastro quoted the most relevant parts of it.

The C ++ C ++ specification must be purchased . A draft is available for free.

    
10.11.2014 / 11:35
13

The answer varies according to language. In the case of C #, the signature of a method is made up of:

  • the method name
  • the number of generic parameters (ex: T )
  • the number of parameters
  • parameter modifiers (ex: out , ref )
  • the parameter types.

The return type, generic constraints (ex: where T: IDisposable ) and parameter names are not part of the signature.

For overloading :

  • types object and dynamic are considered equal;
  • modifiers this and params are ignored;
  • The signatures of two methods belonging to the same type can not differ only by the modifiers ref and out . That is, the following 2 methods can not coexist in the same type:

    void Method(out i);
    void Method(ref i);
    

    But they can be declared in different types in the same hierarchy tree ( method hiding and overriding ).

From section 1.6.6 of the specification:

  

The signature of a method must be unique in the class in which the method is declared. The signature of a   method 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.

Section 3.6:

  

Although out and ref parameter modifiers are considered part of a signature, members declared in a single   type can not differ in signature only by ref and out

     

For the purposes of singatures, the types object and dynamic are considered the same. Members declared in a   single type can not differ in signature only by object and dynamic .

However, although other elements are not part of the signature of a method (eg name of the parameters), they all contribute to the public API and change them is considered breaking change . p>     

11.11.2014 / 10:35
3

The signature of a method is given by the number and types of arguments of the method, as well as by its return value.

    
11.11.2014 / 00:14