Quoting from an answer from this question. Guid is a value type, so a variable of type Guid can't be null to start with.
The GUID data type is a text string representing a Class identifier (ID). COM must be able to convert the string to a valid Class ID. All GUIDs must be authored in uppercase. The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).
A Guid is a struct , those can't be null.
Empty is "{00000000-0000-0000-0000-000000000000}" which located the representation range of a guid, but we just marked is as Empty, so it is safe to use (someGuid ==Guid. Empty).
Use the static method Guid.NewGuid()
instead of calling the default constructor.
var responseObject = proxy.CallService(new RequestObject
{
Data = "misc. data",
Guid = Guid.NewGuid()
});
Lessons to learn from this:
1) Guid is a value type, not a reference type.
2) Calling the default constructor new S()
on any value type always gives you back the all-zero form of that value type, whatever it is. It is logically the same as default(S)
.
Try this instead:
var responseObject = proxy.CallService(new RequestObject
{
Data = "misc. data",
Guid = new Guid.NewGuid()
});
This will generate a 'real' Guid value. When you new a reference type, it will give you the default value (which in this case, is all zeroes for a Guid).
When you create a new Guid, it will initialize it to all zeroes, which is the default value for Guid. It's basically the same as creating a "new" int (which is a value type but you can do this anyways):
Guid g1; // g1 is 00000000-0000-0000-0000-000000000000
Guid g2 = new Guid(); // g2 is 00000000-0000-0000-0000-000000000000
Guid g3 = default(Guid); // g3 is 00000000-0000-0000-0000-000000000000
Guid g4 = Guid.NewGuid(); // g4 is not all zeroes
Compare this to doing the same thing with an int:
int i1; // i1 is 0
int i2 = new int(); // i2 is 0
int i3 = default(int); // i3 is 0
Try doing:
Guid foo = Guid.NewGuid();
Can't tell you how many times this has caught. me.
Guid myGuid = Guid.NewGuid();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With