Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More or less equal overloads

Tags:

c#

overloading

The following code compiles in C# 4.0:

void Foo(params string[] parameters) { }
void Foo(string firstParameter, params string[] parameters) { }

How does the compiler know which overload you're calling? And if it can't, why does the code still compile?

like image 676
Jouke van der Maas Avatar asked Jun 26 '10 20:06

Jouke van der Maas


People also ask

Can we overload == operator?

Overloading Binary Operators Suppose that we wish to overload the binary operator == to compare two Point objects. We could do it as a member function or non-member function. To overload as a member function, the declaration is as follows: class Point { public: bool operator==(const Point & rhs) const; // p1.

Does overloading == also overload !=?

NO. There is no such requirement that you Must overload != If you need to overload == . However,it is a good practice that you Should overload operators related to each other.

Which function overloads the == to operator?

In Python, overloading is achieved by overriding the method which is specifically for that operator, in the user-defined class. For example, __add__(self, x) is a method reserved for overloading + operator, and __eq__(self, x) is for overloading == .

What is operator overloading overload <= operator with example?

Operator Overloading in C++ This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.


1 Answers

It is well specified in the C# Language Specification, chapter 7.4.3.2, "Better function member":

Otherwise, if MP is applicable in its normal form and MQ has a params array and is applicable only in its expanded form, then MP is better than MQ

Otherwise, if MP has fewer declared parameters than MQ, then MP is better than MQ. This can occur if both methods have params arrays and are applicable only in their expanded forms.

Fwiw, the C# Language Specification is a very readable document and can help you resolve these puzzles by yourself. You have it on your machine, find it back in the Visual Studio install directory (like c:\program files\microsoft visual studio 9.0) in the vc#\specifications\1033 subdirectory.

Another good one is the Ecma-335 standard document, freely available as a PDF download. It specifies the behavior of the CLR and the JIT compiler, great material to understand why C# (and the CLR) do what they do. Recommended.

like image 51
Hans Passant Avatar answered Oct 05 '22 18:10

Hans Passant