0

Given this example code, is there a way for the method foo to determine at run-time that the 4th parameter hes been defaulted?

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);
        }
    }
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
nicomp
  • 4,344
  • 4
  • 27
  • 60
  • the better [solution](https://stackoverflow.com/questions/20886811/check-inside-method-whether-some-optional-argument-was-passed) is to overloads the method like in the first answer. – Simo Sep 17 '18 at 15:25

1 Answers1

0

No

You could take some liberty and guess that if it is the default value, it probably wasn't given, but there is no way to know for sure.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142