方法是否可以在运行时确定是否默认设置了参数?

时间:2018-09-17 15:19:01

标签: c# methods optional-parameters

给出此示例代码,方法 foo 是否可以在运行时确定默认设置的第四个参数?

using System;

namespace DefaultParameters {
    class Program {
        static void Main(string[] args) {
            int a = 0,b = 1,c = 3;
            foo(a, b, c);       // The 4th parm is defaulted in the method definition.
            foo(a, b, c, 100);  // The 4th parm is explicit in the method call.
        }

        /// <summary>
        /// A method with a default paramater
        /// </summary>
        /// <param name="speed">Speed</param>
        /// <param name="brightness">Brightness</param>
        /// <param name="weight">Weight</param>
        /// <param name="humidity">Humidity</param>
        public static void foo(int speed, int brightness, int weight, int humidity = 42) {
            Console.WriteLine("speed = " + speed + " brightness = " + brightness + " weight = " + weight + " humidity  = " + humidity);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以自由一些,然后猜测如果的默认值,则可能未给出该默认值,但是无法确定。