Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C# calls different for overloaded methods for different values of the same type?

I have one doubt concerning c# method overloading and call resolution.

Let's suppose I have the following C# code:

enum MyEnum { Value1, Value2 }

public void test() {
    method(0); // this calls method(MyEnum)
    method(1); // this calls method(object)
}

public void method(object o) {
}

public void method(MyEnum e) {
}

Note that I know how to make it work but I would like to know why for one value of int (0) it calls one method and for another (1) it calls another. It sounds awkward since both values have the same type (int) but they are "linked" for different methods.

like image 330
Fabio Veronez Avatar asked Mar 25 '10 12:03

Fabio Veronez


People also ask

Why are we using C?

C is highly portable and is used for scripting system applications which form a major part of Windows, UNIX, and Linux operating system. C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc.

Why is C used in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Why is C so popular language?

The C programming language is so popular because it is known as the mother of all programming languages. This language is widely flexible to use memory management. C is the best option for system level programming language.


1 Answers

Literal 0 is implicitly convertible to any enum type, which is a closer match than object. Spec.

See, for example, these blog posts.

like image 82
SLaks Avatar answered Oct 22 '22 09:10

SLaks