Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 514 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I default a parameter to Guid.Empty in C#?

#1
I wish to say:


public void Problem(Guid optional = Guid.Empty)
{
}


But the compiler complains that Guid.Empty is not a compile time constant.

As I don’t wish to change the API I can’t use:

Nullable<Guid>
Reply

#2
The compiler is quite correct; `Guid.Empty` is not a compile-time constant. You can try making a method overload like this:

public void Problem()
{
Problem(Guid.Empty);
}
Reply

#3
`Guid.Empty` is equivalent to `new Guid()`, which is equivalent to `default(Guid)`. So you can use:

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

or

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

Note that the `new Foo()` value is *only* applicable when:

- You're really calling the *parameterless* constructor
- `Foo` is a value type

In other words, when the compiler knows it's really just the default value for the type :)

(Interestingly, I'm 99.9% sure it *won't* call any custom `new Foo()` constructor you may have created. You can't create such a constructor in a value type in C#, but you *can* do so in IL.)

You can use the `default(Foo)` option for *any* type.
Reply

#4
Can't you use:

`default ( Guid )` ?
Reply

#5
The accepted answer does not work in ASP.NET MVC, and cause this 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)' ....

Instead, you may do the following:

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

#6
## Solution

### You can use `new Guid()` instead

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

### You can also use `default(Guid)`

`default(Guid)` also will work exactly as `new Guid()`.

Because Guid is a value type not reference type, so, `default(Guid)` is not equal to `null` for example, instead, it's equal to calling default constructor.

Which means that this:

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

It's exactly the same as the original example.

## Explanation

### Why didn't `Guid.Empty` work?

The reason you are getting the error is because `Empty` is defined as:

public static readonly Guid Empty;

So, it is a variable, not a constant (defined as `static readonly` not as `const`). Compiler can only have compiler-known values as method parameters default values (not runtime-only-known).

The root cause is that you cannot have a `const` of any `struct`, unlike `enum` for example. If you try it, it will not compile.

The reason once more is that `struct` is not a primitive type.
For a list of all primitive types in .NET see

[To see links please register here]

(note that `enum` usually inherits `int`, which is a primitive)

### But `new Guid()` is not a constant too!

I'm not saying it needs a constant. It needs something that can be decided in compile time. `Empty` is a field, so, it's value is not known in compile time (only at very beginning of run time).

Default parameter value must be known at compile-time, which may be a `const` value, or something defined using a C# feature that makes value known at compile time, like `default(Guid)` or `new Guid()` (which is decided at compile time for `struct`s as you cannot modify the `struct` constructor in code).

While you can provide `default` or `new` easily, you cannot provide a `const` (because it's not a primitive type or an `enum` as explained above). So, again, not saying that optional parameter itself needs a constant, but compiler known value.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through