C# how to create a Guid value?

后端 未结 11 2112
旧时难觅i
旧时难觅i 2020-11-27 09:58

One field of our struct is Guid type. How to generate a valid value for it?

相关标签:
11条回答
  • 2020-11-27 10:27

    If you are using this in the Reflection C#, you can get the guid from the property attribute as follows

    var propertyAttributes= property.GetCustomAttributes();
    foreach(var attribute in propertyAttributes)
    {
      var myguid= Guid.Parse(attribute.Id.ToString());
    }
    
    
    
    0 讨论(0)
  • 2020-11-27 10:28
    Guid id = Guid.NewGuid();
    
    0 讨论(0)
  • 2020-11-27 10:30

    Guid.NewGuid() will create one

    0 讨论(0)
  • 2020-11-27 10:35

    To makes an "empty" all-0 guid like 00000000-0000-0000-0000-000000000000.

    var makeAllZeroGuID = new System.Guid();
    

    or

    var makeAllZeroGuID = System.Guid.Empty;
    

    To makes an actual guid with a unique value, what you probably want.

    var uniqueGuID = System.Guid.NewGuid(); 
    
    0 讨论(0)
  • 2020-11-27 10:37

    If you want to create a "desired" Guid you can do

    var tempGuid = Guid.Parse("<guidValue>");
    

    where <guidValue> would be something like 1A3B944E-3632-467B-A53A-206305310BAE.

    0 讨论(0)
  • 2020-11-27 10:38

    There are two ways

    var guid = Guid.NewGuid();
    

    or

    var guid = Guid.NewGuid().ToString();
    

    both use the Guid class, the first creates a Guid Object, the second a Guid string.

    0 讨论(0)
提交回复
热议问题