Home  >  Article  >  Backend Development  >  What does @ mean in c#

What does @ mean in c#

下次还敢
下次还敢Original
2024-05-09 22:36:201030browse

The @ symbol in C# cancels the escaped meaning of special characters, allowing strings to span multiple lines, Enum members to customize the underlying value, and to indicate that a method or property is asynchronous. The @ symbol in

What does @ mean in c#

c# The @ symbol in

c# is a modifier used to unescape The special meaning of the sequence.

Function

  • When the @ symbol is placed in front of a string, the conversion of special characters (such as \n, \t, etc.) in the string can be canceled. righteous meaning. This allows the string to span multiple lines without having to use escape sequences.
  • When the @ symbol is placed in front of an Enum member, its underlying value can be specified. This allows Enum members to have custom values ​​without being restricted by integers.
  • When the @ symbol is placed in front of a method or property, it can indicate that the method or property is asynchronous.

String

The following are examples of strings with and without the @ symbol:

<code class="c#">// 不带 @ 符号
string s = "Hello\nWorld!";

// 带 @ 符号
string s = @"Hello
World!";</code>

In the first example , \n generates a newline character. But in the second example, \n is output as is due to the presence of the @ symbol.

Enum

Here are examples of Enum with and without the @ symbol:

<code class="c#">// 不带 @ 符号
enum Weekdays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

// 带 @ 符号
enum Weekdays { Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7 }</code>

In the first example, the underlying Weekdays Values ​​are integers starting from 0. But in the second example, the @ symbol allows specifying custom values ​​for the Enum members.

Asynchronous methods and properties

The following are examples of asynchronous methods and properties with and without the @ symbol:

<code class="c#">// 不带 @ 符号
Task<int> GetNumber() { ... }

// 带 @ 符号
Task<int> GetNumberAsync() { ... }</code>

In the first In the example, GetNumber is a synchronous method. In the second example, the @ symbol indicates that GetNumberAsync is an asynchronous method.

The above is the detailed content of What does @ mean in c#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn