Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

c#

guid

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.

like image 921
Dave Avatar asked Nov 09 '09 11:11

Dave


People also ask

Is a GUID a string?

A GUID is a 16-byte (128-bit) number, typically represented by a 32-character hexadecimal string.

Can you convert a GUID to an int?

An integer uses 32 bits, whereas a GUID is 128 bits - therefore, there's no way to convert a GUID into an integer without losing information, so you can't convert it back.

What does GUID parse do?

The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. This method can convert strings in any of the five formats produced by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

How long is a GUID string?

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.


1 Answers

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)
like image 123
Vadim Gremyachev Avatar answered Sep 23 '22 12:09

Vadim Gremyachev