Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '+' cannot be applied to operands of type "string" and "method group" [closed]

Tags:

string

c#

I am checking to see if a Directory exists with this code:

while (Directory.Exists(currentDirectory + year.ToString))
{
  // do stuff
  year++;
}

year is a normal integer, currentDirectory a string. Unfortunately this Operation gives me the "Operator '+' cannot be applied to operands of type "string" and "method group" error mesage. I don't really want to create a new string on each Iteration when I only need to increment.

like image 220
Haris Avatar asked Feb 27 '14 11:02

Haris


2 Answers

ToString is a method. You need to invoke it; so you're missing () after ToString.

Change it to

while (Directory.Exists(currentDirectory + year.ToString()))
{
    // do stuff
    year++;
}

And it should work :)

like image 181
khellang Avatar answered Sep 30 '22 19:09

khellang


Missing brackets () after ToString. You need to change it to the following:

while (Directory.Exists(currentDirectory + year.ToString()))
{
  // do stuff
  year++;
}
like image 36
XN16 Avatar answered Sep 30 '22 17:09

XN16