20

I am using C# .net 3.5 to build an application. I have been working with optional parameter attributes in .net 4.0 with no problems. I did notice that with 3.5 there is the option (workaround) to add the following attributes to your method like so:

    public static void MethodName(string name, [Optional][DefaultValue(null)]string placeHolder)
    {

    }

Even though I have added the attributes to the method, if I try and call it like so:

     MethodName("test");

The compiler will complain that it is looking for two parameters instead of one. Is it actually possible to do this using C# .net 3.5? Am I doing something wrong?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Deano
  • 2,805
  • 3
  • 29
  • 42

3 Answers3

35

Optional parameters are C# 4.0 language feature so it doesn't matter which framework you are targeting, but you have to compile it using VS 2010 or newer.

Use this syntax in VS 2010 or newer:

public static void MethodName(string name, string placeHolder = null)
{
    // body
}

Or this in older one:

public static void MethodName(string name, string placeHolder)
{
    // body
}

public static void MethodName(string name)
{
    MethodName(name, null);
}
rotman
  • 1,641
  • 1
  • 12
  • 25
  • Yeah, I tried setting the framework version to 3.5 in VS2010, but it still throws a compilation error. "Arguments Mismatch". – Deano Feb 25 '11 at 14:04
  • I was also looking for a way to do this without having to do it the old skool way - looks like I might have to! – Deano Feb 25 '11 at 14:05
  • In project properties go to build tab, advanced and set the language to C# 4.0. That should work even when .NET 3.5 framework is set. – rotman Feb 25 '11 at 14:32
  • I tried setting the framework version to 4 & 3.5 in VS2010, but it still throws a compilation error when I use it in VS2008. "Arguments Mismatch" – Deano Feb 25 '11 at 16:26
  • And oh - you mentioned you use it in VS2008. It can't work. Only VS2010 can compile C# 4.0 code. That's what I wrote in my answer. – rotman Feb 25 '11 at 19:10
6

The Optional attribute has been available since C# 1.0, and is used when interoperating with external code, it has no effect on method calls in your own code.

As there is no optional parameters in C# 3, you can use overloading instead:

public static void MethodName(string name, string placeHolder) {
  ...
}

public static void MethodName(string name) {
  MethodName(name, null);
}

(Side note: There is no C# version 3.5, that is a framework version.)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
4

Take a look at the following StackOverflow thread: C# Optional Parameters in .net 3.5

No use in copy pasting everything which has been said there, as the thread covers pretty much everything. Good luck.

Community
  • 1
  • 1
Jens
  • 3,249
  • 2
  • 25
  • 42
  • Yeah, I did see that question - unfortunately I tried setting the framework version to 3.5 in VS2010, but it still throws a compilation error. "Arguments Mismatch" – Deano Feb 25 '11 at 14:05