Optional arguments and InteropServices

2019-02-19 07:44发布

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?

1条回答
贼婆χ
2楼-- · 2019-02-19 08:10

Method 1 was present since .NET 1.1.

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

查看更多
登录 后发表回答