Nice programing

C #에서 문자열 앞의 @는 무엇입니까?

nicepro 2020. 10. 3. 11:47
반응형

C #에서 문자열 앞의 @는 무엇입니까?


이것은 C # (또는 아마도 VB.net)에 대한 .NET 질문이지만 다음 선언의 차이점을 알아 내려고 노력하고 있습니다.

string hello = "hello";

string hello_alias = @"hello";

콘솔에 인쇄해도 차이가 없으며 길이 속성은 동일합니다.


문자열을 축어 문자열 리터럴 로 표시합니다. 일반적으로 이스케이프 시퀀스 로 해석되는 문자열의 모든 항목 은 무시됩니다.

그래서 "C:\\Users\\Rich"같은@"C:\Users\Rich"

한 가지 예외가 있습니다. 큰 따옴표에는 이스케이프 시퀀스가 ​​필요합니다. 큰 따옴표를 이스케이프하려면 두 개의 큰 따옴표를 한 행에 넣어야합니다. 예를 들어 @""""".


그것은 A의 축 어적으로 문자열 리터럴 . 이스케이프가 적용되지 않음을 의미합니다. 예를 들면 :

string verbatim = @"foo\bar";
string regular = "foo\\bar";

여기 verbatimregular같은 내용이 있습니다.

또한 여러 줄 내용을 허용하므로 SQL에 매우 편리합니다.

string select = @"
SELECT Foo
FROM Bar
WHERE Name='Baz'";

축어 문자열 리터럴에 필요한 이스케이프의 한 비트는 두 배로하는 큰 따옴표 ( ")를 얻는 것입니다.

string verbatim = @"He said, ""Would you like some coffee?"" and left.";
string regular = "He said, \"Would you like some coffee?\" and left.";

'@'는 또 다른 의미가 있습니다. 변수 선언 앞에 놓으면 예약 된 키워드를 변수 이름으로 사용할 수 있습니다.

예를 들면 :

string @class = "something";
int @object = 1;

나는 이것에 대한 합법적 인 용도를 한두 가지만 찾았습니다. 주로 다음과 같은 작업을 원할 때 ASP.NET MVC에서 :

<%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>

다음과 같은 HTML 링크가 생성됩니다.

<a href="/Controller/Action" class="some_css_class">Text</a>

그렇지 않으면 예약 된 키워드는 아니지만 대문자 'C'가 HTML 표준을 따르지 않고 제대로 보이지 않는 'Class'를 사용해야합니다.


VB도 명시 적으로 요청 했으므로이 축어 문자열 구문은 VB에는 존재하지 않고 C #에만 존재한다는 점을 추가하겠습니다. 오히려 모든 문자열은 VB에서 그대로입니다 (C # 축약 문자열과 달리 줄 바꿈을 포함 할 수 없다는 사실 제외).

Dim path = "C:\My\Path"
Dim message = "She said, ""Hello, beautiful world."""

이스케이프 시퀀스는 VB에 존재하지 않으므로 (C # 축어 문자열에서와 같이 따옴표 문자가 두 배가되는 경우는 제외) 몇 가지를 더 복잡하게 만듭니다. 예를 들어 VB에서 다음 코드를 작성하려면 연결 (또는 문자열을 구성하는 다른 방법)을 사용해야합니다.

string x = "Foo\nbar";

VB에서는 다음과 같이 작성됩니다.

Dim x = "Foo" & Environment.NewLine & "bar"

( &VB 문자열 연결 연산자입니다. +동일하게 사용할 수 있습니다.)


http://msdn.microsoft.com/en-us/library/aa691090.aspx

C #은 일반 문자열 리터럴과 축어 문자열 리터럴의 두 가지 형식의 문자열 리터럴을 지원합니다.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.


This is a verbatim string, and changes the escaping rules - the only character that is now escaped is ", escaped to "". This is especially useful for file paths and regex:

var path = @"c:\some\location";
var tsql = @"SELECT *
            FROM FOO
            WHERE Bar = 1";
var escaped = @"a "" b";

etc


Copied from MSDN:

At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string @"C:\files.txt" will appear in the watch window as "C:\\files.txt".


Putting a @ in front of a string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters.

So you can write:

string path = @"C:\My path\";

instead of:

string path = "C:\\My path\\";

The explanation is simple. To represent the string "string\", the compiler needs "string\\" because \ is an escape character. If you use @"string\" instead, you can forget about \\.

참고URL : https://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c

반응형