如何在 C# 中将 GUID 转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1700361/
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
How to convert a GUID to a string in C#?
提问by Dave
I'm new to C#.
我是 C# 的新手。
I know in vb.net, i can do this:
我知道在 vb.net 中,我可以这样做:
Dim guid as string = System.Guid.NewGuid.ToString
In C#, I'm trying to do
在 C# 中,我正在尝试做
String guid = System.Guid.NewGuid().ToString;
but i get an "Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?" error.
但我收到“无法将方法组‘ToString’转换为非委托类型‘字符串’。您是否打算调用该方法?” 错误。
采纳答案by Blindy
You're missing the ()
after ToString
that marks it as a function call vs. a function reference (the kind you pass to delegates), which incidentally is why c# has no AddressOf
operator, it's implied by how you type it.
您错过了()
之后将ToString
其标记为函数调用与函数引用(您传递给委托的那种),顺便说一句,这就是为什么 c# 没有AddressOf
运算符的原因,它是由您键入它的方式所暗示的。
Try this:
尝试这个:
string guid = System.Guid.NewGuid().ToString();
回答by BennyM
String guid = System.Guid.NewGuid().ToString();
Otherwise it's a delegate.
否则它是一个代表。
回答by Stephen Newman
You need
你需要
String guid = System.Guid.NewGuid().ToString();
回答by Shiraz Bhaiji
you are missing () on the end of ToString.
您在 ToString 的末尾缺少 ()。
回答by Makach
Did you write
你写了吗
String guid = System.Guid.NewGuid().ToString;
or
或者
String guid = System.Guid.NewGuid().ToString();
notice the paranthesis
注意括号
回答by Thomas
In Visual Basic, you can call a parameterless method without the braces (()
). In C#, they're mandatory. So you should write:
在 Visual Basic 中,您可以调用不带大括号 ( ()
) 的无参数方法。在 C# 中,它们是强制性的。所以你应该写:
String guid = System.Guid.NewGuid().ToString();
Without the braces, you're assigning the method itself (instead of its result) to the variable guid
, and obviously the method cannot be converted to a String
, hence the error.
没有大括号,您将方法本身(而不是其结果)分配给变量guid
,显然该方法无法转换为 a String
,因此出现错误。
回答by Vadim Gremyachev
According to MSDNthe method Guid.ToString(string format)
returns a string representation of the value of this Guid instance, according to the provided format specifier.
根据MSDN,该方法Guid.ToString(string format)
根据提供的格式说明符返回此 Guid 实例值的字符串表示形式。
Examples:
例子:
guidVal.ToString()
orguidVal.ToString("D")
returns 32 digits separated by hyphens:00000000-0000-0000-0000-000000000000
guidVal.ToString("N")
returns 32 digits:00000000000000000000000000000000
guidVal.ToString("B")
returns 32 digits separated by hyphens, enclosed in braces:{00000000-0000-0000-0000-000000000000}
guidVal.ToString()
或guidVal.ToString("D")
返回由连字符分隔的 32 位数字:00000000-0000-0000-0000-000000000000
guidVal.ToString("N")
返回 32 位数字:00000000000000000000000000000000
guidVal.ToString("B")
返回由连字符分隔的 32 位数字,括在大括号中:{00000000-0000-0000-0000-000000000000}
回答by hadi sorosh
Guid guidId = Guid.Parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
string guidValue = guidId.ToString("D"); //return with hyphens
回答by Drew Noakes
Here are examples of output from each of the format specifiers:
以下是每个格式说明符的输出示例:
N: cd26ccf675d64521884f1693c62ed303
D: cd26ccf6-75d6-4521-884f-1693c62ed303
B: {cd26ccf6-75d6-4521-884f-1693c62ed303}
P: (cd26ccf6-75d6-4521-884f-1693c62ed303)
X: {0xcd26ccf6,0x75d6,0x4521,{0x88,0x4f,0x16,0x93,0xc6,0x2e,0xd3,0x03}}
The default is D
.
默认为D
。
回答by Platypus
Following Sonar rules, you should whenever you can try to protect yourself, and use
System.globalisation
whenever it's possible like for DateTime.ToString()
.
遵循声纳规则,你应该在任何时候尝试保护自己,并System.globalisation
在可能的情况下使用
for DateTime.ToString()
。
So regarding the other answers you could use:
因此,关于您可以使用的其他答案:
guid.ToString("", CultureInfo.InvariantCulture)
where ""
can be replaces by : N, D, B , P and X for more infos see this comment.
其中""
可以替换为:N、D、B、P 和 X 了解更多信息,请参阅此评论。
Example here
示例在这里