163

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?

d219
  • 2,707
  • 5
  • 31
  • 36
anchor
  • 1,685
  • 2
  • 11
  • 11

5 Answers5

197

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)

Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
53

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

Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
31

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()

Task
  • 3,668
  • 1
  • 21
  • 32
David Watts
  • 2,249
  • 22
  • 33
  • 9
    @anchor I've noticed that you have never accepted any answer to your questions. [Here - in the FAQ of StackExchange](http://meta.stackexchange.com/q/5234) you can read how you can accept an answer and why you should do that. – Genhis Jul 20 '15 at 04:47
  • 3
    @Genhis I think he got what he needed and then dropped of the face of the earth haha – David Watts Feb 14 '19 at 16:33
12

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.

tinamou
  • 2,282
  • 3
  • 24
  • 28
0

You can create an Empty Guid or New Guid using a class.

The default value of Guid is 00000000-0000-0000-0000-000000000000

public class clsGuid  // This is a class name
{
    public Guid MyGuid { get; set; }
}

static void Main(string[] args)
{
    clsGuid cs = new clsGuid();   
    Console.WriteLine(cs.MyGuid); // This will give empty Guid "00000000-0000-0000-0000-000000000000"

    cs.MyGuid = new Guid();
    Console.WriteLine(cs.MyGuid); // This will also give empty Guid "00000000-0000-0000-0000-000000000000"

    cs.MyGuid = Guid.NewGuid();
    Console.WriteLine(cs.MyGuid); // This way, it will give a new Guid (eg. "d94828f8-7fa0-4dd0-bf91-49d81d5646af")

    Console.ReadKey(); // This line holds the output screen in a console application
}
zcoop98
  • 2,590
  • 1
  • 18
  • 31
THE LIFE-TIME LEARNER
  • 1,476
  • 1
  • 8
  • 18
  • yes...you can make the class value as static value...or using that in class.. – THE LIFE-TIME LEARNER Jan 10 '20 at 06:11
  • 1
    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. – nvoigt Jan 10 '20 at 06:13
  • 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... – THE LIFE-TIME LEARNER Jan 10 '20 at 06:24
  • 1
    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. – The Thirsty Ape Aug 13 '21 at 16:09