Guid is all 0's (zeros)?

后端 未结 6 1215
忘掉有多难
忘掉有多难 2020-12-05 01:35

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         


        
6条回答
  •  执念已碎
    2020-12-05 02:17

    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
    

提交回复
热议问题