一个方法可以在运行时确定参数是否已被默认? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

给定这个示例代码,方法foo有没有办法在运行时确定第4个参数是否已被默认?

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);
        }
    }
}
c# methods optional-parameters
1个回答
0
投票

No

你可以采取一些自由,并猜测如果它是默认值,它可能没有给出,但没有办法确定。

© www.soinside.com 2019 - 2024. All rights reserved.