Dave
Dave

Reputation: 1950

How to convert a GUID to a string in C#?

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.

Upvotes: 96

Views: 260944

Answers (10)

Makach
Makach

Reputation: 7569

Did you write

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

or

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

notice the parenthesis.

Upvotes: 9

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

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)

Upvotes: 152

Platypus
Platypus

Reputation: 371

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

Upvotes: 2

Drew Noakes
Drew Noakes

Reputation: 311305

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.

Upvotes: 27

hadi sorosh
hadi sorosh

Reputation: 41

Guid guidId = Guid.Parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
string guidValue = guidId.ToString("D"); //return with hyphens

Upvotes: 4

Blindy
Blindy

Reputation: 67487

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();

Upvotes: 133

Stephen Newman
Stephen Newman

Reputation: 1977

You need

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

Upvotes: 7

Thomas
Thomas

Reputation: 182038

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.

Upvotes: 9

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65461

you are missing () on the end of ToString.

Upvotes: 5

BennyM
BennyM

Reputation: 2856

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

Otherwise it's a delegate.

Upvotes: 6

Related Questions