C# 中方法過載的不同方法是什麼?


一個方法可以過載的不同方法有 −

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

以下給出了一個說明不同資料型別引數的示例 −

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);
}

以下說明了不同的引數數量 −

// 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;
}

更新於: 20-06-2020

175 瀏覽

開啟您的 職業生涯

完成課程後獲取認證

開始操作
廣告