tags:

views:

596

answers:

6

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.

+5  A: 
String guid = System.Guid.NewGuid().ToString();

Otherwise it's a delegate.

BennyM
+2  A: 

You need

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

Stephen Newman
+3  A: 

you are missing () on the end of ToString.

Shiraz Bhaiji
+10  A: 

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.

You want to do this:

String guid = System.Guid.NewGuid().ToString();
Blindy
you seem to have done the same...edited for you.
James
Aren't you missing the, too? ;)
Bobby
+1  A: 

Did you write

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

or

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

notice the paranthesis

Makach
+3  A: 

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.

Thomas