Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Signature in C#

Tags:

c#

What is the Method Signature in the following

int DoSomething(int a, int b); 

Return type is a part of signature or not???

like image 521
Usman Avatar asked Jan 10 '12 18:01

Usman


People also ask

What is a method signature?

The method signature in java is defined as the structure of the method that is designed by the programmer. The method signature is the combination of the method name and the parameter list. The method signature depicts the behavior of the method i.e types of values of the method, return type of the method, etc.

How do you write a signature for a method?

In Java programming language, the method signature consists of two parts: the method's name and the parameter list. These two parts are part of a method declaration. The parameter list includes the number, type, and order of parameters but not the name of the parameter.

Why do we need method signature?

In Java, a method signature is part of the method declaration. It's the combination of the method name and the parameter list. The reason for the emphasis on just the method name and parameter list is because of overloading. It's the ability to write methods that have the same name but accept different parameters.

What is the signature of a function in C ++?

What is function signature in c ++? A function signature consists of the functions name and number of, order of, type of parameters used. myFunc(int a, int b) anotherFunc(char x, float y) The parts above would be the functions signature minus the names of the parameter variables.


2 Answers

Return type is not part of the method signature in C#. Only the method name and its parameters types (but not the parameter names) are part of the signature. You cannot, for example, have these two methods:

int DoSomething(int a, int b); string DoSomething(int a, int b); 

To be clear: Methods cannot be overloaded based on their return type. They must have a unique name, unique parameter types, or pass their arguments differently (e.g. using out or ref).

Edit: To answer your original question, the method signature for your method is:

DoSomething(int, int) 

Note that this all applies to normal methods. If you're talking about delegates, then you should see keyboardP's answer. (Short version: return type IS part of the signature for a delegate).

like image 121
ean5533 Avatar answered Sep 28 '22 10:09

ean5533


Is the return type is a part of signature or not?

It depends on why you are asking the question. Why do you care?

There are two definitions of method signature. The C# language definition does not include the return type, and uses the signature of the method to determine whether two overloads are allowed. Two methods with the same signature are not allowed in a type. Since C# does not consider the return type to be a part of the signature, C# does not allow two methods that differ only in return type to be declared in the same type.

The CLR, however, does include the return type in the signature. The CLR allows for two methods to be in the same type that differ only in return type.

To be more specific: in C# the signature consists of the methods:

  • name
  • number of type parameters
  • number of formal parameters
  • type of each formal parameter
  • out/ref/value-ness of each formal parameter

with the following additional notes:

  • generic type parameter constraints are not part of the signature
  • return type is not part of the signature
  • type parameter and formal parameter names are not part of the signature
  • two methods may not differ only in out/ref

In the CLR the signature consists of:

  • name
  • number of type parameters
  • number of formal parameters
  • type of each formal parameter including modopts and modreqs
  • return type including modopts and modreqs
  • ref/value-ness of each formal parameter

Note that the CLR does not distinguish between "ref int" and "out int" at all when considering signatures. Note that the CLR does distinguish between modopt/modreq types. (The way that the C# compiler deals with modopt/modreq types is too complex to summarize here.)

like image 44
Eric Lippert Avatar answered Sep 28 '22 08:09

Eric Lippert