Say I have two overloaded versions of a C# method:
void Method( TypeA a ) { }
void Method( TypeB b ) { }
I call the method with:
Method( null );
Which overload of the method is called? What can I do to ensure that a particular overload is called?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.
It depends on TypeA
and TypeB
.
null
to TypeB
because it's a value type but TypeA
is a reference type) then the call will be made to the applicable one.TypeA
and TypeB
.
TypeA
to TypeB
but no implicit conversion from TypeB
to TypeA
then the overload using TypeA
will be used.TypeB
to TypeA
but no implicit conversion from TypeA
to TypeB
then the overload using TypeB
will be used.See section 7.4.3.4 of the C# 3.0 spec for the detailed rules.
Here's an example of it not being ambiguous. Here TypeB
derives from TypeA
, which means there's an implicit conversion from TypeB
to TypeA
, but not vice versa. Thus the overload using TypeB
is used:
using System;
class TypeA {}
class TypeB : TypeA {}
class Program
{
static void Foo(TypeA x)
{
Console.WriteLine("Foo(TypeA)");
}
static void Foo(TypeB x)
{
Console.WriteLine("Foo(TypeB)");
}
static void Main()
{
Foo(null); // Prints Foo(TypeB)
}
}
In general, even in the face of an otherwise-ambiguous call, to ensure that a particular overload is used, just cast:
Foo((TypeA) null);
or
Foo((TypeB) null);
Note that if this involves inheritance in the declaring classes (i.e. one class is overloading a method declared by its base class) you're into a whole other problem, and you need to cast the target of the method rather than the argument.
Jon Skeet has given a comprehensive answer, but from a design point of view you shouldn't depend on corner-cases of the compiler specification. If nothing else, if you have to look up what it does before you write it, the next person to try to read it won't know what it does either. It's not maintainable.
Overloads are there for convenience, and two different overloads with the same name should do the same thing. If the two methods do different things, rename one or both of them.
It's more usual for an overloaded method to have variants with varying numbers of parameters, and for the overload with less parameters to supply sensible defaults.
e.g. string ToString(string format, System.IFormatProvider provider)
has the most parameters,string ToString(System.IFormatProvider provider)
supplies a default format, andstring ToString()
supplies a default format and provider,
Jon Skeet already answered which overload gets chosen by default, but if you want to ensure that particular overload is called, it is often better to use named parameters than cast.
If you have:
void Method( TypeA a ) { }
void Method( TypeB b ) { }
You can call Method(a: null);
or Method(b: null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With