如何在 C# 中将参数默认为 Guid.Empty? [英] How can I default a parameter to Guid.Empty in C#?
问题描述
我想说:
public void Problem(Guid optional = Guid.Empty)
{
}
但是编译器抱怨 Guid.Empty 不是编译时常量.
But the compiler complains that Guid.Empty is not a compile time constant.
因为我不想更改 API,所以我无法使用:
As I don’t wish to change the API I can’t use:
Nullable<Guid>
推荐答案
解决方案
您可以使用 new Guid()
代替
public void Problem(Guid optional = new Guid())
{
// when called without parameters this will be true
var guidIsEmpty = optional == Guid.Empty;
}
你也可以使用default(Guid)
default(Guid)
也将与 new Guid()
完全一样工作.
You can also use default(Guid)
default(Guid)
also will work exactly as new Guid()
.
因为 Guid 是值类型而不是引用类型,所以,例如 default(Guid)
不等于 null
,而是等于调用默认构造函数.
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.
这意味着:
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.
您收到错误的原因是因为 Empty
定义为:
The reason you are getting the error is because Empty
is defined as:
public static readonly Guid Empty;
所以,它是一个变量,而不是一个常量(定义为 static readonly
而不是 const
).编译器只能将编译器已知的值作为方法参数的默认值(而不是仅运行时已知的).
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).
根本原因是您不能拥有任何 struct
的 const
,例如与 enum
不同.如果你尝试,它不会编译.
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.
原因再次是 struct
不是原始类型.
有关 .NET 中所有原始类型的列表,请参阅 http://msdn.microsoft.com/en-gb/library/system.typecode.aspx
(注意enum
通常会继承int
,这是一个原语)
The reason once more is that struct
is not a primitive type.
For a list of all primitive types in .NET see http://msdn.microsoft.com/en-gb/library/system.typecode.aspx
(note that enum
usually inherits int
, which is a primitive)
我并不是说它需要一个常数.它需要一些可以在编译时决定的东西.Empty
是一个字段,因此,它的值在编译时是未知的(仅在运行时开始时).
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).
必须在编译时知道默认参数值,它可以是 const
值,或者使用 C# 特性定义的东西,在编译时知道值,如 default(Guid)
或 new Guid()
(这是在编译时为 struct
决定的,因为您不能修改代码中的 struct
构造函数).
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).
虽然您可以轻松提供 default
或 new
,但您不能提供 const
(因为它不是原始类型或 enum
如上所述).所以,再说一次,不是说可选参数本身需要一个常量,而是编译器已知的值.
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.
这篇关于如何在 C# 中将参数默认为 Guid.Empty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!