Guid.Parse() or new Guid() - What's the difference?

后端 未结 5 1436
悲哀的现实
悲哀的现实 2020-12-29 19:24

What is the difference between these two ways of converting a string to System.Guid? Is there a reason to choose one over the other?

var myguid          


        
5条回答
  •  隐瞒了意图╮
    2020-12-29 19:38

    Use the version that is the most readable to you. The two are implemented almost exactly the same way.

    The only real difference is that the constructor initializes itself to Guid.Empty before attempting the parse. However, the effective code is identical.

    That being said, if the Guid is coming from user input, then Guid.TryParse would be better than either option. If this Guid is hard coded, and always valid, either of the above are perfectly reasonable options.

提交回复
热议问题