1

C# 関数内の名前付きパラメーターが欠落している場合、コンパイラーは、関数内の欠落している各パラメーターの名前を出力する代わりに、欠落している引数の数のみを出力します。

prog.cs(10,27): error CS1501: No overload for method `createPrism' takes `2' arguments`.

ただし、デバッグの目的で、特に多くのパラメーターを受け取る関数の場合、関数呼び出しで欠落しているパラメーターの名前を取得する必要があることがよくあります。C# 関数呼び出しで欠落しているパラメーターを出力することは可能ですか?

using System;
public class Test{
    public static int createPrism(int width, int height, int length, int red, int green, int blue, int alpha){
        //This function should print the names of the missing parameters
        //to the error console if any of its parameters are missing. 

        return length*width*height;
    }
    static void Main(){
        Console.WriteLine(getVolume(length: 3, width: 3));
        //I haven't figured out how to obtain the names of the missing parameters in this function call.
    }
}
4

2 に答える 2

1

すべてのパラメータを null 可能にして、デフォルト値を設定できます。次に、メソッドで、null 値を持つパラメーターを確認し、それについて何かを行うことができます。

   public static int createPrism(int? width=null, int? height=null){
       if(!height.HasValue){
         Console.Write("height parameter not set")
       }
   }
于 2013-07-01T22:32:07.607 に答える