What is the default value for Guid?

147,323

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.

Share:
147,323
anchor
Author by

anchor

Updated on July 08, 2022

Comments

  • anchor
    anchor almost 2 years

    The default value for int is 0 , for string is "" and for boolean it is false. Could someone please clarify what the default value for guid is?

  • Genhis
    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
    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
    Akhilesh Singh over 4 years
    yes...you can make the class value as static value...or using that in class..
  • nvoigt
    nvoigt over 4 years
    I 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
    Akhilesh Singh over 4 years
    yes...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
    Uwe Keim almost 3 years
    In newer C# versions, default(Guid) and default are the same, too.
  • The Thirsty Ape
    The Thirsty Ape almost 3 years
    Formatting 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
    JSWilson almost 3 years
    Guid.Empty can't be used as a compile time constant. So you can't use it to default a parameter.