ASP.NET символ

Я пытаюсь следовать некоторым учебным руководствам для ASP.NET и ни за что в жизни, я просто не могу выяснить что @ Символ делает, когда это перед переменной.

Я думал, что это был просто ярлык или на переменные сеанса или на request.form, но я попробовал его в нескольких местах без любой удачи.

Когда я вставил его где-нибудь наугад, я получаю ошибку: Expression Expected, однако, когда я смотрю на примеры, я работаю от, они не похожи на выражения, таким образом, я очень смущен!

Помогите!?

8
задан John Saunders 15 December 2009 в 12:07
поделиться

3 ответа

The @ symbol in C# allows you to use a keyword as a variable name.

For instance:

//this will throw an exception, in C# class is a keyword
string class = "CSS class name"; 

//this won't
string @class = "CSS class name"; 

Usually it's best to avoid using keywords as variable names, but sometimes it's more elegant than having awkward variable names. You tend to most often see them when serialising stuff for the web and in anon types.

Your error is probably due to applying the @ before a variable name that isn't a keyword.

Update:

In T-SQL @ is always used before parameter names, for instance:

select * 
from [mytable]
where [mytable].[recId] = @id

You would then specify the @id parameter when you called the query.

8
ответ дан 5 December 2019 в 08:24
поделиться

There are several different uses for the @ symbol, depending where it is.

In front of a variable name, it allows you to use a reserved word as a variable name:

string @string = "a string variable named string";

This is not good practice as it can be very confusing when reading code.

In front of a string, it is called a verbatim string literal and means that you do not need to escape slashes and such:

string path = @"c:\my path\is here";
string normal_path = "c:\\my path\\is here";
7
ответ дан 5 December 2019 в 08:24
поделиться

На странице ASPX символ @ используется вместе с директивами страницы.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

На кодовой странице символ @ используется для управляющих символов строковых значений.

String s = @"c:/Document/Files/Sample.txt"
5
ответ дан 5 December 2019 в 08:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: