You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Content/Chapter-10-methods/methods-overloading/methods-overloading.md
+7-5
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,16 @@
1
1
# Method Overloading
2
2
3
-
In many programming languages like C# and Java the same method can be declared in **few variants** with the same name and different parameters. This goes by the term “**method overloading**”. Now let's see how to write these overloaded methods in C#.
3
+
In many programming languages like C# and Java the same method can be declared in **few variants** with the **same name and different parameters**. This goes by the term “**method overloading**”. Now let's see how to write these overloaded methods in C#.
4
4
5
5
## Method Signature
6
6
7
-
In programming methods are **identified** through the elements of their declaration: **name** of the method + a list of its **parameters**. These two elements define its specification, the so called “**method signature**”. It is defined by the **method name** and the **definitions of the method parameters** (their types).
7
+
In programming methods are **identified** through the elements of their declaration: **name** of the method + a list of its **parameters**. These two elements define its specification, the so called “**method signature**”.
8
+
9
+
The **method signature** is defined by the **method name** and the **definitions of the method parameters** (only parameter types are considered, and the parameter names are ignored). Example:
In this example the method's signature is its name (**`Print`**), together with its parameter (**`string text`**).
13
+
In this example the method's signature is its name (**`Print`**), together with its parameter types (**`string`**).
12
14
13
15
If our program holds several **methods with the same name**, but with **different lists of parameters (signatures)**, we can say that we use **method overloading**.
14
16
@@ -20,9 +22,9 @@ As we mentioned, if you use **the same name for several methods with different s
20
22
21
23
## Signature and Return Value Type
22
24
23
-
It is important to say that **the returned type as a result** of the method **is not a part of its signature**. If the returned type was a part of the signature, then the compiler doesn't know which method exactly to call.
25
+
It is important to say that **the returned type as a result** of the method **is not a part of its signature**. If the returned type was a part of the signature, then the compiler doesn't know which method exactly to call (there is an ambiguity).
24
26
25
-
Let's look at the following example – we have two methods with different return types. Despite that, Visual Studio shows that there is a mistake, because both of their signatures are the same. Therefore, when trying to call a method named **`Print(…)`**, the compiler can't know which of the two methods to run.
27
+
Let's look at the following **example**: we have two methods with different return types. Despite that, Visual Studio shows that there is a mistake, because both of their signatures are the same. Therefore, when trying to call a method named **`Print(…)`**, the compiler can't know which of the two methods to invoke.
0 commit comments