Computer >> Máy Tính >  >> Lập trình >> C#

Các cách khác nhau để một phương thức được nạp chồng trong C # là gì?

Các cách khác nhau mà một phương thức có thể được nạp chồng là -

The datatypes of parameters are different
The number of parameters are different

Phần sau đưa ra một ví dụ nêu rõ các kiểu dữ liệu khác nhau của các tham số -

void print(int i) {
   Console.WriteLine("Printing int: {0}", i );
}

void print(double f) {
   Console.WriteLine("Printing float: {0}" , f);
}

void print(string s) {
   Console.WriteLine("Printing string: {0}", s);
}

Phần sau cho biết số lượng tham số khác nhau -

// two parameters
public static int mulDisplay(int one, int two) {
   return one * two;
}

// three parameters
public static int mulDisplay(int one, int two, int three) {
   return one * two * three;
}

// four parameters
public static int mulDisplay(int one, int two, int three, int four) {
   return one * two * three * four;
}