danyolgiax
danyolgiax

Reputation: 13096

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?

Upvotes: 3

Views: 1003

Answers (1)

Etienne de Martel
Etienne de Martel

Reputation: 36984

Method 1 was present since .NET 1.1.

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

Upvotes: 3

Related Questions