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

Dave picture Dave · Nov 9, 2009 · Viewed 156.5k times · Source

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.

Answer

Blindy picture Blindy · Nov 9, 2009

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