如何在 C# 中将 GUID 转换为字符串?

发布于 2024-08-10 18:15:54 字数 302 浏览 5 评论 0原文

我是 C# 新手。

我知道在 vb.net 中,我可以这样做:

Dim guid as string = System.Guid.NewGuid.ToString

在 C# 中,我正在尝试这样做

String guid = System.Guid.NewGuid().ToString;

,但我得到一个“无法将方法组 'ToString' 转换为非委托类型 'string'。您是否打算调用方法?”错误。

I'm new to C#.

I know in vb.net, i can do this:

Dim guid as string = System.Guid.NewGuid.ToString

In C#, I'm trying to do

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

断舍离 2024-08-17 18:15:54

根据 MSDN 方法 Guid.ToString(string format) 根据提供的格式说明符返回此 Guid 实例的值的字符串表示形式。

示例:

  • 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}
  • guidVal.ToString( "P") 返回由连字符分隔的 32 个十六进制数字,括在括号中:(00000000-0000-0000-0000-000000000000)

According to MSDN the method Guid.ToString(string format) returns a string representation of the value of this Guid instance, according to the provided format specifier.

Examples:

  • guidVal.ToString() or guidVal.ToString("D") returns 32 hex digits
    separated by hyphens: 00000000-0000-0000-0000-000000000000
  • guidVal.ToString("N") returns 32 hex digits:00000000000000000000000000000000
  • guidVal.ToString("B") returns 32 hex digits separated by hyphens, enclosed in braces:{00000000-0000-0000-0000-000000000000}
  • guidVal.ToString("P") returns 32 hex digits separated by hyphens, enclosed in parentheses: (00000000-0000-0000-0000-000000000000)
娇俏 2024-08-17 18:15:54

您缺少 ToString 之后的 () ,它将其标记为函数调用与函数引用(传递给委托的类型),顺便说一句,这就是 c# 的原因没有 AddressOf 运算符,它是由您键入的方式暗示的。

试试这个:

string guid = System.Guid.NewGuid().ToString();

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.

Try this:

string guid = System.Guid.NewGuid().ToString();
徒留西风 2024-08-17 18:15:54

以下是每个格式说明符的输出示例:

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}}

默认为 D

自行运行

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.

Run this yourself.

蘑菇王子 2024-08-17 18:15:54

在 Visual Basic 中,可以调用不带大括号的无参数方法 (())。在 C# 中,它们是强制性的。所以你应该这样写:

String guid = System.Guid.NewGuid().ToString();

如果没有大括号,你将把方法本身(而不是它的结果)分配给变量 guid,并且显然该方法不能转换为 String >,因此出现错误。

In Visual Basic, you can call a parameterless method without the braces (()). In C#, they're mandatory. So you should write:

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.

野却迷人 2024-08-17 18:15:54

您写下

String guid = System.Guid.NewGuid().ToString;

String guid = System.Guid.NewGuid().ToString();

注意到括号了吗?

Did you write

String guid = System.Guid.NewGuid().ToString;

or

String guid = System.Guid.NewGuid().ToString();

notice the parenthesis.

小忆控 2024-08-17 18:15:54

你需要

String guid = System.Guid.NewGuid().ToString();

You need

String guid = System.Guid.NewGuid().ToString();
左秋 2024-08-17 18:15:54
String guid = System.Guid.NewGuid().ToString();

否则就是代表。

String guid = System.Guid.NewGuid().ToString();

Otherwise it's a delegate.

丶情人眼里出诗心の 2024-08-17 18:15:54

ToString 末尾缺少 () 。

you are missing () on the end of ToString.

年华零落成诗 2024-08-17 18:15:54
Guid guidId = Guid.Parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
string guidValue = guidId.ToString("D"); //return with hyphens
Guid guidId = Guid.Parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
string guidValue = guidId.ToString("D"); //return with hyphens
半世蒼涼 2024-08-17 18:15:54

遵循声纳规则,只要可以尝试保护自己,您就应该使用
只要有可能,System.globalization 就像 DateTime.ToString() 一样。

因此,关于您可以使用的其他答案:

guid.ToString("", CultureInfo.InvariantCulture)

其中 "" 可以替换为: N、D、B、P 和 X 有关更多信息,请参阅 此评论

示例 此处

Following Sonar rules, you should whenever you can try to protect yourself, and use
System.globalisation whenever it's possible like 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.

Example here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文