Why are optional parameters just compiled into equivalent overloads in C #?

Possible duplicate:
method overload with additional parameter in C # 4.0

It seems that most of the drawbacks of options, such as version issues, could be resolved by making optional parameters simply converted to overloads. Is there a technical reason why the optional C # parameters are not implemented in a way that boils down to overloads?

+4
source share
1 answer

One reason is that if the compiler automatically compiled the optional parameters in overloads, this would be contrary to the ability of developers to define them themselves. For example, the following code is legal.

class Container { public void Example(int x) { ... } public void Example(int x, int y = 42) { ... } } 
+2
source

Source: https://habr.com/ru/post/1391047/


All Articles