The default value for int is 0 , for string is "" and for boolean is false. May someone please clarify what is the default value for guid?
int的默认值为0,对于string为“”,对于boolean为false。请有人请说明guid的默认值是什么?
4 个解决方案
#1
56
You can use these methods to get an empty guid. The result will be a guid with all it's digits being 0's - "00000000-0000-0000-0000-000000000000
".
您可以使用这些方法获取空guid。结果将是一个guid,其全部数字为0 - “00000000-0000-0000-0000-000000000000”。
new Guid()
default(Guid)
Guid.Empty
#2
22
You can use Guid.Empty
. It is a read-only instance of the Guid structure with the value of 00000000-0000-0000-0000-000000000000
你可以使用Guid.Empty。它是Guid结构的只读实例,值为00000000-0000-0000-0000-000000000000
you can also use these instead
你也可以用它们代替
var g = new Guid();
var g = default(Guid);
beware not to use Guid.NewGuid()
because it will generate a new Guid.
注意不要使用Guid.NewGuid(),因为它会生成一个新的Guid。
use one of the options above which you and your team think it is more readable and stick to it. Do not mix different options across the code. I think the Guid.Empty
is the best one since new Guid()
might make us think it is generating a new guid and some may not know what is the value of default(Guid)
.
使用上面的一个选项,您和您的团队认为它更具可读性并坚持下去。不要在代码中混合使用不同的选项。我认为Guid.Empty是最好的,因为新的Guid()可能会让我们认为它正在产生一个新的guid,有些人可能不知道默认值(Guid)是什么。
#3
10
The default value for a GUID is empty. (eg: 00000000-0000-0000-0000-000000000000)
GUID的默认值为空。 (例如:00000000-0000-0000-0000-000000000000)
This can be invoked using Guid.Empty()
or new Guid()
这可以使用Guid.Empty()或new Guid()调用
If you want a new GUID, you use Guid.NewGuid()
如果你想要一个新的GUID,你可以使用Guid.NewGuid()
#4
3
To extend answers above, you cannot use Guid default value with Guid.Empty
as an optional argument in method, indexer or delegate definition, because it will give you compile time error. Use default(Guid)
or new Guid()
instead.
要扩展上面的答案,您不能将Guid.Empty的Guid默认值用作方法,索引器或委托定义中的可选参数,因为它会给您编译时错误。使用默认(Guid)或新Guid()代替。
#1
56
You can use these methods to get an empty guid. The result will be a guid with all it's digits being 0's - "00000000-0000-0000-0000-000000000000
".
您可以使用这些方法获取空guid。结果将是一个guid,其全部数字为0 - “00000000-0000-0000-0000-000000000000”。
new Guid()
default(Guid)
Guid.Empty
#2
22
You can use Guid.Empty
. It is a read-only instance of the Guid structure with the value of 00000000-0000-0000-0000-000000000000
你可以使用Guid.Empty。它是Guid结构的只读实例,值为00000000-0000-0000-0000-000000000000
you can also use these instead
你也可以用它们代替
var g = new Guid();
var g = default(Guid);
beware not to use Guid.NewGuid()
because it will generate a new Guid.
注意不要使用Guid.NewGuid(),因为它会生成一个新的Guid。
use one of the options above which you and your team think it is more readable and stick to it. Do not mix different options across the code. I think the Guid.Empty
is the best one since new Guid()
might make us think it is generating a new guid and some may not know what is the value of default(Guid)
.
使用上面的一个选项,您和您的团队认为它更具可读性并坚持下去。不要在代码中混合使用不同的选项。我认为Guid.Empty是最好的,因为新的Guid()可能会让我们认为它正在产生一个新的guid,有些人可能不知道默认值(Guid)是什么。
#3
10
The default value for a GUID is empty. (eg: 00000000-0000-0000-0000-000000000000)
GUID的默认值为空。 (例如:00000000-0000-0000-0000-000000000000)
This can be invoked using Guid.Empty()
or new Guid()
这可以使用Guid.Empty()或new Guid()调用
If you want a new GUID, you use Guid.NewGuid()
如果你想要一个新的GUID,你可以使用Guid.NewGuid()
#4
3
To extend answers above, you cannot use Guid default value with Guid.Empty
as an optional argument in method, indexer or delegate definition, because it will give you compile time error. Use default(Guid)
or new Guid()
instead.
要扩展上面的答案,您不能将Guid.Empty的Guid默认值用作方法,索引器或委托定义中的可选参数,因为它会给您编译时错误。使用默认(Guid)或新Guid()代替。