C#中字符串前面的@是什么? [英] What's the @ in front of a string in C#?

查看:21
本文介绍了C#中字符串前面的@是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个针对 C#(或可能是 VB.net)的 .NET 问题,但我试图找出以下声明之间的区别:

This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations:

string hello = "hello";

对比

string hello_alias = @"hello";

在控制台打印出来没有区别,长度属性是一样的.

Printing out on the console makes no difference, the length properties are the same.

推荐答案

它将字符串标记为 逐字字符串文字 - 字符串中通常被解释为转义序列的任何内容被忽略.

It marks the string as a verbatim string literal - anything in the string that would normally be interpreted as an escape sequence is ignored.

所以 "C:\Users\Rich"@"C:UsersRich"

有一个例外:双引号需要转义序列.要转义双引号,您需要连续放置两个双引号.例如,@"""" 的计算结果为 ".

There is one exception: an escape sequence is needed for the double quote. To escape a double quote, you need to put two double quotes in a row. For instance, @"""" evaluates to ".

这篇关于C#中字符串前面的@是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆