Search code examples
c#optional-arguments

Optional arguments and InteropServices


I'm using for the firs time optional arguments but I cannot understand difference between those two method definitions:

private void method1([Optional, DefaultParameterValue(string.Empty)] string testString)
{
    //do something
}

private void method2(string testString = "")
{
    //do something
}

definition of method1 needs:

using System.Runtime.InteropServices;

Method2 definition is smaller and needs no import.

Have I to consider something before using one of those method syntax?


Solution

  • Method 1 was present since .NET 1.1.

    Method 2 was introduced with C# 4 (C# did not support optional parameters until then).