Home >Backend Development >C++ >What Does the `@` Symbol Do Before a Variable Name in C#?

What Does the `@` Symbol Do Before a Variable Name in C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-21 10:26:09411browse

What Does the `@` Symbol Do Before a Variable Name in C#?

What is the function of the @ symbol before the variable name in C#?

In C#, the @ symbol in front of a variable name has a specific purpose.

Usage:

Using the @ symbol before a variable name allows reserved words to be used as variable identifiers. Reserved words (for example, "class", "int", and "string") are keywords in the language that have special meaning and cannot be used for other purposes, including as variable names.

Example:

Consider the following code:

<code class="language-csharp">int @class = 15;</code>

Here, the @ symbol allows the reserved word "class" to be used as a variable name. This code will successfully compile and execute, assigning the value 15 to the variable named "@class".

Without the @ symbol, the compiler will throw an error because "class" is a reserved word and cannot be used directly as a variable name:

<code class="language-csharp">int class = 15; // 错误:'class' 是保留字,不能用作变量名。</code>

The @ symbol therefore provides a workaround that allows reserved words to be used as variable names when necessary.

The above is the detailed content of What Does the `@` Symbol Do Before a Variable Name 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