开发者

Optional arguments and InteropServices

开发者 https://www.devze.com 2023-03-10 03:53 出处:网络
I\'m using for the firs time optional arguments but I cannot understand difference between those two method definitions:

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;
开发者_如何学JAVA

Method2 definition is smaller and needs no import.

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


Method 1 was present since .NET 1.1.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号