C# default parameters

Posted Jun 16, 20203 min read

cause

The reason for writing this article is to replace the previous method by adding default parameters. It was found that although it is written the same when called, it is not actually treated as the same method. The two methods are roughly as follows:

//The previous method-delete
private static string TestMethod(string first)
{
    return first;
}

//New method with the same name
private static string TestMethod(string first, string second = "2")
{
    return second;
}

Both of the above methods can be called by TestMethod("1");, so at first I mistakenly thought that the call of the two methods is equivalent, but in actual use, the method referenced by DLL will prompt that the method cannot be found , There is a problem here.

First of all, we can make an attempt, we will find that these two methods can exist at the same time, or the above example, then through TestMethod("1"); call will find that the returned result is "1", which is The first method without default parameters

So far, the principle is not yet clear, but you can feel that the writing in the program may be different at the time of calling. At this time, we can go deeper and look at it from the perspective of the intermediate language IL(Intermediate Language)

ILSpy

With a simple example, take a look at commonly used decompilation tools

static void Main(string[]args)
{
    Console.WriteLine(TestMethod("1"));
    Console.ReadKey();

    TestMethodWithDefaultParam(string.Empty);
}

private static string TestMethod(string first)
{
    return first;
}

private static string TestMethod(string first, string second = "2")
{
    return second;
}

private static void TestMethodWithDefaultParam(string first, string second = "2")
{

}

Put the compiled program into ILSpy and decompile it

Focus on the comparison of TestMethodWithDefaultParam This method call, we can find that although we did not pass in the second parameter, but because of the existence of the default parameter, the compiler automatically helped us fill in a parameter, and TestMethod The method is obviously to call the first one without default parameters, and the TestMethod method with default parameters is ignored

ildasm

Through ILSpy, we simply see the difference between the two methods when calling, but the implementation of the default parameters is not yet clear, so we can take a look at the IL disassembler ildasm that comes with VisualStudio, and use the command line tool of VS to enter ildasm Open the program, and then open the exe file of our console program

Through this directory, we can clearly see that the parameter form of the two TestMethod methods, the default parameter method is only to allow us to omit the parameter when calling, and this method still has this parameter in the program, when two methods with the same name If they exist together, it is equivalent to overloading the method. When the corresponding parameter is passed in, the corresponding method is called.

At this time, we open the method with default parameters. In the form of IL, there is a [opt]logo in front of the default parameters, which represents Optional, that is, "optional", which is a feature label of C# OptionalAttribute, through the two features of OptionalAttribute and DefaultParameterValueAttribute, the default parameters of C# are realized. There are already related blog posts in this regard, you can refer to Talk about the realization of the new feature "default parameters" of C# 4.0