Difference between two string values.

I would like to know what the difference is between these two strings - if any. I always thought they were the same, but apparently not.

I was trying to use them to a function today and one string format worked, but the other did not (it simply would not run).  So, behind the scene, there has to be some inherent difference that the C# compiler was seeing.

string dirPath =  @"\\C:\Users\<user name>\Desktop\Test\TEST_DIR\";

string dirPath =  "C:\\Users\\<user name>\\Desktop\\Test\\TEST_DIR\\";

Thanks for your help,
Fulano
Mr_FulanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mankowitzCommented:
at-quoted strings are verbatim, and there are no escape characters, while double quoted strings allow substitutions.

Your two strings above become
\\C:\Users\<user name>\Desktop\Test\TEST_DIR\
C:\Users\<user name>\Desktop\Test\TEST_DIR\

in the at-quoted string, the \\ is not escaped, while it is translated to \ in the second string.
0
Ryan ChongCommented:
string dirPath1 =  @"\\C:\Users\<user name>\Desktop\Test\TEST_DIR\";
and
string dirPath2 =  "C:\\Users\\<user name>\\Desktop\\Test\\TEST_DIR\\";

will contains different values, but if you change a bit the first variable to this they will be the same.

string dirPath1 =  @"C:\Users\<user name>\Desktop\Test\TEST_DIR\";

string dirPath1 =  @"\\C:\Users\<user name>\Desktop\Test\TEST_DIR\";
string dirPath2 =  "C:\\Users\\<user name>\\Desktop\\Test\\TEST_DIR\\";
if (dirPath1 == dirPath2) {
    Console.WriteLine("Both variables are same");   
} else {
     Console.WriteLine("Both variables are different");   
}
string dirPath3 =  @"C:\Users\<user name>\Desktop\Test\TEST_DIR\";
string dirPath4 =  "C:\\Users\\<user name>\\Desktop\\Test\\TEST_DIR\\";
if (dirPath3 == dirPath4) {
    Console.WriteLine("Both variables are same");   
} else {
     Console.WriteLine("Both variables are different");   
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_FulanoAuthor Commented:
Both answers were excellent!!! Thank you both for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.