C#学习 @字符的作用

如果不想去识别字符串中的转义字符,可以在字符串前加@符号,这样除了双引号其他转义字符都不会再识别。

一个字符串一般只能定义在一行,但如果想要占用多行定义一个字符串,可以在前面加上@符号,这样可以定义在多行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _005
{
    class Program
    {
        static void Main(string[] args)
        {
            //当在字符串前使用@字符时,可以把一个字符串定义在多行
            //编译器不会再去识别字符串的转义字符
            //如果需要在字符串中表示一个双引号,则需要使用两个双引号
            string str1 =@"I'm a good man.
You are bad girl!";
            Console.WriteLine(str1);
            string str2 = @"I'm a good man.\n""You are bad girl!";
            Console.WriteLine(str2);


            //使用@字符的第二个好处
            string path = "c:\\xxx\\xx\\xxx.doc";
            Console.WriteLine(path);
            string path2=@"c:\xxx\xx\xxx.doc";
            Console.WriteLine(path2);
            Console.ReadKey();

        }
    }
}


猜你喜欢

转载自blog.csdn.net/q418030645/article/details/78760124