2

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?

4

1 回答 1

3

方法 1 从 .NET 1.1 开始存在。

方法 2 是在 C# 4 中引入的(C# 直到那时才支持可选参数)。

于 2011-06-02T14:17:00.367 回答