如何在 C# 中将 GUID 转换为字符串?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
根据 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()
orguidVal.ToString("D")
returns 32 hex digitsseparated 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)
您缺少
ToString
之后的()
,它将其标记为函数调用与函数引用(传递给委托的类型),顺便说一句,这就是 c# 的原因没有AddressOf
运算符,它是由您键入的方式暗示的。试试这个:
You're missing the
()
afterToString
that marks it as a function call vs. a function reference (the kind you pass to delegates), which incidentally is why c# has noAddressOf
operator, it's implied by how you type it.Try this:
以下是每个格式说明符的输出示例:
默认为
D
。自行运行。
Here are examples of output from each of the format specifiers:
The default is
D
.Run this yourself.
在 Visual Basic 中,可以调用不带大括号的无参数方法 (
()
)。在 C# 中,它们是强制性的。所以你应该这样写:如果没有大括号,你将把方法本身(而不是它的结果)分配给变量
guid
,并且显然该方法不能转换为String
>,因此出现错误。In Visual Basic, you can call a parameterless method without the braces (
()
). In C#, they're mandatory. So you should write: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 aString
, hence the error.您写下
或
注意到括号了吗?
Did you write
or
notice the parenthesis.
你需要
You need
否则就是代表。
Otherwise it's a delegate.
ToString 末尾缺少 () 。
you are missing () on the end of ToString.
遵循声纳规则,只要可以尝试保护自己,您就应该使用
只要有可能,
System.globalization
就像DateTime.ToString()
一样。因此,关于您可以使用的其他答案:
其中
""
可以替换为: 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 forDateTime.ToString()
.So regarding the other answers you could use:
where
""
can be replaces by : N, D, B , P and X for more infos see this comment.Example here