Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# not consider the return type of a function in polymorphism?

There are two functions with same name and same set of parameters but with different return types. Why is it not a form of polymorphism i.e. method overloading? Why is it not allowed by compiler?

like image 941
Manish Dubey Avatar asked Mar 22 '14 20:03

Manish Dubey


People also ask

Why does letter C exist?

Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.

Does the letter C need to exist?

This is a very important rule considering about 25% of words in our language contain a C.] So why do we need a C? When we combine the C with an H we DO make a unique sound. Without a C we would go to Hurch instead of Church, we would listen to a Hime instead of a Chime, etc.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

Why does C make two sounds?

In the Latin-based orthographies of many European languages, including English, a distinction between hard and soft ⟨c⟩ occurs in which ⟨c⟩ represents two distinct phonemes. The sound of a hard ⟨c⟩ often precedes the non-front vowels ⟨a⟩, ⟨o⟩ and ⟨u⟩, and is that of the voiceless velar stop, /k/ (as in car).

Why should I learn C?

Moreover, a lot of the principles used in C – for instance, argc and argv for command line parameters, as well as loop constructs and variable types – will show up in a lot of other languages you learn so you’ll be able to talk to people even if they don’t know C in a way that’s common to both of you.

What does %C mean in C programming?

While it's an integer, the %c interprets its numeric value as a character value for display. For instance for the character a: If you used %d you'd get an integer, e.g., 97, the internal representation of the character a using %c to display the character ' a ' itself (if using ASCII)

Why is C such a popular programming language?

This means that, instead of generating machine code for every architecture to be supported, compilers for those languages just generate intermediate C code, and the C compiler handles the machine code generation. C has also become a lingua franca for communicating between developers.

Why is the world powered by C?

Many C projects are still started today; there are some good reasons for that. How is the World Powered by C? Despite the prevalence of higher-level languages, C continues to empower the world. The following are some of the systems that are used by millions and are programmed in the C language.


2 Answers

Because C# is designed so that types can be analyzed from inside to outside. Imagine if you have

int N() {}
float N()() {}

and then a call

float x = N();

OK, great, obviously we could say that the float version was wanted. But then you say:

void M(int x) {}
void M(float x) {}

M(N());

OK, now which version was wanted? The rule is figure out what N() means and then figure out what the best overload of M is once you know what N() means. You reason from inside to outside.

Overload resolution based on return type requires reasoning from outside to inside and that can be a lot harder.

like image 88
Eric Lippert Avatar answered Feb 06 '23 04:02

Eric Lippert


Consider the following:

int combine(int a, int b) {
    return a + b;
}

float combine(int a, int b) {
    return a - b;
}

If I were to call combine(1, 2), there is no way for the compiler to know which of the two methods I want to call (it's ambiguous).

You could almost make a case for checking return types, but what about:

var c = combine(1, 2);
dynamic d = combine(1, 2);
combine(1, 2);

In the above, what should the value of c or d be? 3? -1? It's impossible to tell. How about the last statement, where there's no value being assigned? I didn't define an overload that returns void, so which of the two methods should it call?

like image 29
Acorn1010 Avatar answered Feb 06 '23 05:02

Acorn1010