我怎样才能默认在C#中的参数Guid.Empty? [英] How can I default a parameter to Guid.Empty in C#?

查看:4851
本文介绍了我怎样才能默认在C#中的参数Guid.Empty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想说的:

 公共无效的问题(GUID可选= Guid.Empty)
{
}

但是,编译器抱怨Guid.Empty不是编译时间常数。



由于我不希望改变我无法使用API​​:

 可空<&的Guid GT; 


解决方案

请它新的GUID( )代替。

 公共无效的问题(GUID可选=新的GUID())
{
//当不带参数叫,这将是真正的
VAR guidIsEmpty =可选的== Guid.Empty;
}



另一种解决方案:



默认(GUID)也将工作完全为新的GUID()



由于GUID是值类型不是引用类型,因此,默认(GUID)不等于<$ C 。$ C>空例如,相反,它是等于调用默认的构造函数



这意味着这样的:

 公共无效的问题(GUID可选=默认(GUID))
{
//当不带参数叫,这将是真正的
VAR guidIsEmpty =可选的== Guid.Empty;
}



这是完全一样的原来的例子。



根本原因:



您所得到的是错误的原因是因为空缺定义为:

 公共静态只读的Guid空; 



所以,它是一个变量,而不是一个常数(定义为静态只读不是常量)。编译器只能编译器已知值作为方法参数的默认值(不运行时仅知名的)。



的根本原因是,你不能有一个常量中的任何结构,不像枚举为例。如果您尝试它,它不会编译。



原因再次是结构不是一个原始类型。结果
。对于所有基本类型在.NET中的列表,请参阅的http:/ /msdn.microsoft.com/en-gb/library/system.typecode.aspx 结果
(注意,枚举通常继承int或。一样)



说明:



我并不是说,它需要一个不变。它需要的东西,可以在编译时决定。 空缺是一个领域,因此,它的价值不是在编译时(仅在运行时十分开始)称。



默认参数值必须在编译时是已知的,这可能是一个常量价值,或者说使用C#的功能,使在编译时已知值来定义,如默认(GUID)新的GUID()(这是在编译时间结构决定能够像您不能修改代码的结构构造函数)。



虽然你可以提供默认容易,你不能提供一个常量 (因为它不是一个基本类型或一个枚举如上所述)。所以,再次,不是说可选参数本身需要一个常数,而是编译器已知值。


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>

解决方案

Make it new Guid() instead.

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

Alternative solution:

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.

Root Cause:

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 http://msdn.microsoft.com/en-gb/library/system.typecode.aspx
(note that enum usually inherits int or alike).

Explanation:

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 structs 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.

这篇关于我怎样才能默认在C#中的参数Guid.Empty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆