314

I'm testing out some WCF services that send objects with Guids back and forth. In my web app test code, I'm doing the following:

var responseObject = proxy.CallService(new RequestObject
{
    Data = "misc. data",
    Guid = new Guid()
});

For some reason, the call to new Guid() is generating Guids with all 0's (zeros) like this:

00000000-0000-0000-0000-000000000000

What could be causing this?

2

6 Answers 6

518

Use the static method Guid.NewGuid() instead of calling the default constructor.

var responseObject = proxy.CallService(new RequestObject
{
    Data = "misc. data",
    Guid = Guid.NewGuid()
});
1
  • 24
    +1 for the correct answer as well as a link to the proper documentation. Nov 1, 2011 at 20:06
122

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

3
  • 3
    Does it compile into the same IL as default(S) or are there any subtleties I'm missing? Nov 2, 2011 at 19:02
  • 8
    @configurator: It does. In fact, the compiler's internal representation of "default(S)" and "new S()" are the same; we do not distinguish them internally, which has led to some unfortunate bugs over the years because they are in fact not quite identical. For example, const int x = new int(); is not supposed to be legal according to the spec, but const int x = default(int); is; we allow both. Nov 7, 2011 at 19:04
  • 1
    @configurator - if you're interested in related corner cases, perhaps msmvps.com/blogs/jon_skeet/archive/2008/12/10/… would also be of interest.
    – kvb
    Nov 7, 2011 at 19:46
60

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
9
  • 1
    g1 will only compile as field and not as local variable. Also the indices in your comment column don't match up with the same line of the code Nov 1, 2011 at 22:02
  • 1
    @CodeInChaos: Thanks, fixed the comments. FYI, the g1 line does actually compile...
    – JohnD
    Nov 1, 2011 at 23:43
  • 3
    It will compile as is, but it has no defined value. If you add code that reads it (before writing to it) it will not compile anymore. Nov 1, 2011 at 23:58
  • 1
    Right, good point, you will get an error if you use an uninitialized variable, so the value cannot be used.
    – JohnD
    Nov 2, 2011 at 1:35
  • 2
    There's a 'new' too many in 'Guid = new Guid.NewGuid()', the 4th line of the first example. It must have wound up in there in an edit, because I can't believe it took 8 years for someone to notice that. And I think there's something wrong with the next paragraph, "when you 'new' a reference type, it will give you the default value". Guid is a value type.
    – Luc VdV
    Apr 23, 2019 at 8:10
33

Try doing:

Guid foo = Guid.NewGuid();
1
  • 1
    Reason for downvote: "Guid" is a type but is used like a variable. Jan 17, 2019 at 18:16
19

Can't tell you how many times this has caught. me.

Guid myGuid = Guid.NewGuid(); 
12

In the spirit of being complete, the answers that instruct you to use Guid.NewGuid() are correct.

In addressing your subsequent edit, you'll need to post the code for your RequestObject class. I'm suspecting that your guid property is not marked as a DataMember, and thus is not being serialized over the wire. Since default(Guid) is the same as new Guid() (i.e. all 0's), this would explain the behavior you're seeing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.