-1

Sometimes I find that many people use @string for locate a directory than use string even I though that they are same. for example : I have variable that called direct

string direct = "C:\\Users";

And then,I type :

System.IO.Directory.CreateDirectory(@direct);

I think it's same with

System.IO.Directory.CreateDirectory(direct);

But,what's difference between @direct and direct?

Yoga Cahya R.
  • 115
  • 2
  • 11

1 Answers1

-1

The @ prefix allows you to use a reserved keyword as a variable name.

The following is an error:

string string = "C:\\Junk";

but the following is allowed (although a very bad idea):

string @string = "C:\\Junk";

The @ prefix can also be used as a verbatim string literal:

string thefolderpath = @"C:\Junk";

If I had to guess, I would say the original programmer decided to use @string, @int etc. for temporary variables, as a style choice. A style choice, it must be said, akin to wearing socks with sandals.

SSS
  • 4,807
  • 1
  • 23
  • 44
  • 2
    If a question is clearly a duplicate, please don't answer it. Just vote to close it as a duplicate. There isn't a situation where it's okay to vote to close *and* answer. – mason Feb 02 '18 at 03:29
  • Actually the linked answer is the wrong one, it should be https://stackoverflow.com/questions/429529/what-does-the-symbol-before-a-variable-name-mean-in-c – SSS Feb 02 '18 at 05:07