Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of equivalent new Guid() and Guid.Empty

Tags:

c#

guid

new Guid() and Guid.Empty produces the same results (all-0 guid (00000000-0000-0000-0000-000000000000).

var guid = new Guid();
Console.WriteLine(guid);//00000000-0000-0000-0000-000000000000
var emptyGuid = Guid.Empty;
Console.WriteLine(emptyGuid);//00000000-0000-0000-0000-000000000000

Is this a two different manners to do the same thing ? readabilty reasons ? or i'm missing something ?

like image 513
hdoghmen Avatar asked Dec 01 '25 05:12

hdoghmen


2 Answers

Guid is a struct. All structs have an implicit default constructor which initializes all members to their default value. In Guid, you see that as setting all it's composite members to 0.

Guid.Empty simply caches the default value via the invocation of the default constructor:

public struct Guid : IFormattable, IComparable, IComparable<Guid>, IEquatable<Guid>
{
    public static readonly Guid Empty = new Guid();
}
like image 104
Yuval Itzchakov Avatar answered Dec 03 '25 17:12

Yuval Itzchakov


Guid is a struct. All structs have an implicit default constructor which initializes all members to their default value. In Guid, you see that as setting all it's composite members to 0.

Guid.Empty simply caches the default value via the invocation of the default constructor:

public struct Guid : IFormattable, IComparable, IComparable<Guid>, IEquatable<Guid>
{
    public static readonly Guid Empty = new Guid();
}
like image 36
Yuval Itzchakov Avatar answered Dec 03 '25 19:12

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!