What is the default value for Guid?
Solution 1
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
".
new Guid()
default(Guid)
Guid.Empty
As Use Keim points out in the comments default
is short for default(Guid)
Solution 2
You can use Guid.Empty
. It is a read-only instance of the Guid structure with the value of 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.
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)
.
Solution 3
The default value for a GUID is empty. (eg: 00000000-0000-0000-0000-000000000000)
This can be invoked using Guid.Empty
or new Guid()
If you want a new GUID, you use Guid.NewGuid()
Solution 4
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.
anchor
Updated on July 08, 2022Comments
-
anchor almost 2 years
The default value for
int
is 0 , forstring
is "" and forboolean
it is false. Could someone please clarify what the default value forguid
is?-
Szer about 9 yearsdefault
Guid
is{00000000-0000-0000-0000-000000000000}
. It's basically binary zeroes. -
Michael Freidgeim over 6 yearsrelated question How can I default a parameter to Guid.Empty in C#?
-
Matthew over 6 yearsThis website can supply empty guids 00000000-0000-0000-0000-000000000000.com
-
Johan Wintgens about 4 yearsAs a side note: the default value of strings is null, not "" (;
-
-
Genhis almost 9 years@anchor I've noticed that you have never accepted any answer to your questions. Here - in the FAQ of StackExchange you can read how you can accept an answer and why you should do that.
-
David Watts over 5 years@Genhis I think he got what he needed and then dropped of the face of the earth haha
-
Akhilesh Singh over 4 yearsyes...you can make the class value as static value...or using that in class..
-
nvoigt over 4 yearsI know we can use Guid as we can use any other type in C#. My question was, whether your answer contains any new information that wasn't here before. It seems all the information you posted was already posted by multiple other people years ago.
-
Akhilesh Singh over 4 yearsyes...u r right...i telling that...u can use empty guid and new guid using a class...that type of answer is not present...in this question...
-
Uwe Keim almost 3 yearsIn newer C# versions,
default(Guid)
anddefault
are the same, too. -
The Thirsty Ape almost 3 yearsFormatting could be improved, specifically the comments should start with //. Otherwise this is a good answer as it specifically displays usage in a custom class.
-
JSWilson almost 3 yearsGuid.Empty can't be used as a compile time constant. So you can't use it to default a parameter.