when to use @ in c#?

前端 未结 5 1155
春和景丽
春和景丽 2020-12-06 04:44

I use @ symbol with local path only, but when do I use @ exactly?

5条回答
  •  悲&欢浪女
    2020-12-06 05:20

    You can prefix a string with the @ sign to avoid having to type 2 backslashes to mean one backslash. This is why it is often used for local path because it saves some typing, and simplifies what you are looking at when you read it later. When you have a bunch of double quotes and other escape characters, aka special characters - that's when you want the @ sign. When you use the @ sign, make sure to put just one backslash when you mean backslash. When using the @ you want to use the double quote character, put two double quotes instead of backslash, double quote.

    String path = "C:\\path\\to\\my\\file";
    

    vs

    String path = @"C:\path\to\my\file"; //@ says one backslash is enough
    

    here is another example:

    String quotation = "He said, \"Wow!\""; //backslashes say not to end string
    

    vs.

    String quotation = @"He said, ""Wow!"""; //need a different method of denoting double quote
    

提交回复
热议问题