9

I've used this helpful post to learn how to pass a list of Enum values as a parameter.

Now I would like to know whether I can make this parameter optional?

Example:

   public enum EnumColors
    {
        [Flags]
        Red = 1,
        Green = 2,
        Blue = 4,
        Black = 8
    }

I want to call my function that receives the Enum param like this:

DoSomethingWithColors(EnumColors.Red | EnumColors.Blue)

OR

DoSomethingWithColors()

My function should then look like what?

public void DoSomethingWithColors(EnumColors someColors = ??)
 {
  ...
  }
Community
  • 1
  • 1
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
  • 3
    As an aside, practically every `enum` should have *some* defined value for `0`, and usually for `Flags` it should be called `None`. – Damien_The_Unbeliever Mar 20 '15 at 08:03
  • 3
    The `[Flags]` attribute belongs on the `enum`, not an enum field. – Sam Axe Mar 20 '15 at 08:05
  • If the value `default(EnumColors)` is the value he wants when the optional argument is omitted, he can use `public void DoSomethingWithColors(EnumColors someColors = 0)` or `public void DoSomethingWithColors(EnumColors someColors = default(EnumColors))`. Or like @Damien_The_Unbeliever said, intrduce `None=0,` in the enum type, and use `public void DoSomethingWithColors(EnumColors someColors = EnumColors.None)`. – Jeppe Stig Nielsen Mar 20 '15 at 08:16

4 Answers4

13

Yes it can be optional.

[Flags]
public enum Flags
{
    F1 = 1,
    F2 = 2
}

public  void Func(Flags f = (Flags.F1 | Flags.F2)) {
    // body
}

You can then call your function with or without parameter. If you call it without any parameter you'll get (Flags.F1 | Flags.F2) as the default value passed to the f parameter

If you don't want to have a default value but the parameter to be still optional you can do

public  void Func(Flags? f = null) {
    if (f.HasValue) {

    }
}
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
6

An enum is a value type, so you could use a nullable value type EnumColors?...

void DoSomethingWithColors(EnumColors? colors = null)
{
    if (colors != null) { Console.WriteLine(colors.Value); }
}

and then set the default value of EnumColors? to null

Another solution is to set EnumColors to an unused value...

void DoSomethingWithColors(EnumColors colors = (EnumColors)int.MinValue)
{
    if (colors != (EnumColors)int.MinValue) { Console.WriteLine(colors); }
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
3

Following code is perfectly valid:

void colorfunc(EnumColors color = (EnumColors.Red | EnumColors.Blue))
{
    //whatever        
}

calling it can be done like this:

colorfunc();
colorfunc(EnumColors.Blue);
Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
0

You can overload your function, so write two functions :

void DoSomethingWithColors(EnumColors colors)
{
    //DoWork
}

void DoSomethingWithColors()
{
    //Do another Work, or call DoSomethingWithColors(DefaultEnum)
}
ZwoRmi
  • 1,093
  • 11
  • 30
  • You have a call from 2009 (?), they asked to tell you that there is. – zerkms Mar 20 '15 at 08:06
  • Since [C# version 4](http://en.wikipedia.org/wiki/C_Sharp_4.0) from 2010, C# does have [optional parameters](https://msdn.microsoft.com/en-us/library/dd264739.aspx). – Jeppe Stig Nielsen Mar 20 '15 at 08:11
  • First : I work in VS2008, and VB.Net have optional parameter, and not C#, that's why i think that. So i've update my answer. Second : My way is more clean (for me), and compatible with previous version. – ZwoRmi Mar 20 '15 at 08:12