CSharp - Method Method Overloading

Introduction

A type may overload methods.

The overloaded methods would have the same name with different signatures.

For example, the following methods can all exist in the same type:

void Test (int x) {}
void Test (double x) {}
void Test (int x, float y) {}
void Test (float x, int y) {}

The following methods cannot coexist in the same type, since the return type and the params modifier are not part of a method's signature:

void  Test (int x) {}
float Test (int x) {}           // Compile-time error
void  Test2 (int[] x) {}
void  Test2 (params int[] x) {}  // Compile-time error

Pass-by-value versus pass-by-reference

Whether a parameter is pass-by-value or pass-by-reference is also part of the signature.

For example, Test(int) can coexist with either Test(ref int) or Test(out int).

However, Test(ref int) and Test(out int) cannot coexist:

void Test (int x) {...}
void Test (ref int x) {...}     // OK so far
void Test (out int x) {...}     // Compile-time error

Related Topics

Quiz