C# 使用 guid 进行测试...如何将变量设置为 Guid?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1367788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:43:51  来源:igfitidea点击:

Testing using a guid... how do I set the variable to a Guid?

c#variables.net-2.0guid

提问by

I am testing some db layer functions. In one, I am simulating the user passing in the id (a GUID). I have hardcoded the guid for testing purposes but can't seem to assign it to a variable, as ridiculous as that sounds. In C# for a .NET 2.0 app. I have tried several ways, all failed. What is the proper way to set a guid to a variable? Here is the code...

我正在测试一些数据库层功能。一方面,我模拟用户传入 id(一个 GUID)。为了测试目的,我对 guid 进行了硬编码,但似乎无法将其分配给变量,这听起来很荒谬。在 C# 中用于 .NET 2.0 应用程序。我尝试了几种方法,都失败了。将 guid 设置为变量的正确方法是什么?这是代码...

Guid x = "5fb7097c-335c-4d07-b4fd-000004e2d28c";

采纳答案by blu

Guid x = new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28c");

回答by Dhanasekar

To create a new GUID

创建新的 GUID

Guid value = Guid.NewGuid(); 

To create a Guidinstance from a string

Guid从字符串创建实例

new Guid("103C8287-30CB-4630-B3F2-978286F72BD7")

To convert a Guidobject to a string

Guid对象转换为字符串

string valueString = value.ToString();