Coder Perfect

In C#, how do I set a parameter to Guid.Empty by default?


I would like to make the following remarks:

public void Problem(Guid optional = Guid.Empty)

The compiler, on the other hand, complains that Guid.Empty isn’t a compile-time constant.

I can’t use: because I don’t want to modify the API.


Asked by Ian Ringrose

Solution #1

public void Problem(Guid optional = new Guid())
  // when called without parameters this will be true
  var guidIsEmpty = optional == Guid.Empty;

default(Guid) will also work in the same way as new Guid ().

Because Guid is a value type rather than a reference type, default(Guid) does not equal null, but rather calls the default constructor.

As a result,

public void Problem(Guid optional = default(Guid))
  // when called without parameters this will be true
  var guidIsEmpty = optional == Guid.Empty;

It’s the same as in the previous case.

The error is caused by the definition of Empty, which is as follows:

public static readonly Guid Empty;

As a result, it is a variable rather than a constant (defined as static readonly not as const). Only compiler-known values can be used as default settings for method arguments (not runtime-only-known).

The main cause is that, unlike enum, you can’t have a const of any struct. It will not compile if you try it.

Again, this is due to the fact that struct is not a primitive type. contains a list of all primitive types in.NET. (Note that enum normally inherits the primitive int)

I’m not claiming that it requires a constant. It requires anything that can be decided throughout the compilation process. Because empty is a field, its value is unknown at compile time (only at very beginning of run time).

The value of the default argument must be known at compile time, which can be a const value or something defined using a C# feature like default(Guid) or new Guid() (which is decided at compile time for structs as you cannot modify the struct constructor in code).

You can easily supply default or new, but not const (since it isn’t a primitive type or an enum, as mentioned above). So, again, I’m not claiming that an optional parameter requires a constant, but rather a compiler-defined value.

Answered by Meligy

Solution #2

Guid. Empty is the same as new Guid(), which is the same as default (Guid). As a result, you can use:

public void Problem(Guid optional = default(Guid))


public void Problem(Guid optional = new Guid())

It’s worth noting that the new Foo() value is only useful when:

In other words, when the compiler realizes it’s merely the type’s default value:)

(Intriguingly, I’m 99.9% certain it won’t invoke any custom new Foo() constructor you’ve written.) In C#, you can’t make such a constructor for a value type, but you can in IL.)

For any type, you can use the default(Foo) option.

Answered by Jon Skeet

Solution #3

Can’t you use:

(Guid) default?

Answered by Nick

Solution #4

The accepted solution does not work with ASP.NET MVC, resulting in the following run-time error:

[ArgumentException: The parameters dictionary contains a null entry for parameter 'optional' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Problem(System.Guid)' ....

You could instead:

public void Problem(Guid? optional)
    if (optional == null)
        optional = new Guid();

Answered by Majix

Solution #5

Guid.Empty is not a compile-time constant, as the compiler claims. You may try something like this for a method overload:

public void Problem()

Answered by user

Post is based on