C# method overloading depend on signature or number of parameters?

Please guide me in C# method over loading: does it depend on method signature, return types or number of parameters ? What changes are essential to say that a method is being or can be overloaded?



It depends of the signature - that is the numbers and types of the arguments.

You can have different overloads with the same number of parameters, but of different types. For example, see the Convert class - many overloads of a method that take one argument but each overload takes a different type.

Overloading takes into account the the number, order and parameters' types. The return type is not considered for overloading purposes

Attention, there is an exception: If a method is overridden in some class c# tries to find a compatible version in the overridden class first and then only will look in the ancestors. For example

Class A
  public virtual void SetProperty(string value)

Class B: Class A
  public override void SetProperty(object value)

If you do the following

  var b= new B();
  b.SetProperty("hello"); // will resolve to overridden method in class B even though you have a more specific version in its parent (class A)

If you mean in terms of overload resolution, it depends on:

  • The name of the method :)
  • The number of parameters
  • The types of the parameters
  • The names of the parameters (if you're using named arguments)
  • Whether parameters are optional or not
  • The number of type parameters

In terms of creating overloaded methods, the names of the parameters and "optionality" aren't relevant, so for example this isn't valid:

// Not valid: same signature
void M(int x, string y)
void M(int z, string a = "")

These have the same signature in terms of name of method, number of type parameters, and the parameter count and types.

A method is said to be overloaded if either the number of parameters is different or the types of parameters is different, but can't be overloaded if only the return type of the method is different (which wont compile).

e.g. below 3 are said to be overloaded, and the 4th one wont be said to be overloaded and wont even compile if other 3 are present:

public int Add(int a, int b); // Method 1, overloaded
public int Add(int a, int b, int c); // Method 2, overloaded
public int Add(double a, double b); // Method 3,  overloaded

public double Add(double a, double b); // Method 4 (invalid, will conflict with Method 3 above)

As per MSDN on Signatures and overloading (C#):

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.

Need Your Help

Seqential & continuous invoice number in Hibernate / ColdFusion ORM?

hibernate orm coldfusion

One of the requirements of my billing software is that the invoice number must be sequential & continuous. (no invoice number can be skipped).

How can I force single threaded behavior in JavaScript?

javascript concurrency callback casperjs

I'm new to JavaScript and I'm just learning to work with its concurrency model. Right now, I have the following code in which I'm trying to implement a busyWait function: