What is a method signature?

Is a signature of the common method/function or constructor composed of its name + amount of parameters + type of the parameters?

Author: Maniero, 2014-11-10

3 answers

Signature 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. Then you need to take advantage of extra information available in the method to make a decision. The most common is to analyze the parameters. If all 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 the return and other information can be analyzed as well, but this is not common since it can bring some problems. Equally understand that order is also important.

Java is one of the languages that went the safe way of parsing only parameters. This is common. C# also went this way. C++ chose to use the return type in some situations as well.

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

Let's look at examples in Java (almost all of them work well in C# as well):

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 execute something (by less) 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 occurred, it would not justify the same name. There is a reasonable chance of the first just calling the second by passing a parameter default (but it doesn't have to be that way). Ex.:

int FazAlgumaCoisa() { FazAlgumaCoisa( 0 ); }

And now look at 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?

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

But I just put the declaration of the methods, I didn't show the declaration of the classes. I considered that these methods are of the same class. This means that you can have methods that appear to have the same signature, but basically 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 way is valid. What many people do not understand is that instance methods have an implicit (hidden) parameter. We can call it this. At the bottom, under the cloths, these classes are assembled 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 use the method that had a 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 it takes in consideration all types, so at the very least you would be comparing a given type to 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 about C# in the dcastro response.

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

The most current C # specification can be found for download or direct access (older version). In his response, dcastro cited the most relevant parts of it.

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

 36
Author: Maniero, 2020-09-10 16:42:07

The answer varies by language. In the case of C#, the signature of a method consists of:

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

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

Concerning overloading :

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

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

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

Of 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.

From Section 3.6:

Although out and ref parameter modifiers are considered part of a signature, members declared in a single type cannot differ in signature solely 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 therefore not differ in signature solely by object and dynamic.

However, although other elements are not part of a method signature (e.g. parameter name), they all contribute for the public API and change them is considered breaking change .

 13
Author: dcastro, 2018-08-27 08:14:24

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

 3
Author: Alfaville, 2014-11-10 23:14:58