开发者

ASP.Net @ Symbol

I am trying to follow some tutorials for ASP.Net and for the life of me, I just can't figure out what the @ Symbol does when it is before an variable.

I thought it was just a shortcut to either session variables or request.form, but I have tried it in a few places without any luck.

When I put it in somewhere at random, I get the error : Expression Expected, however, when I look at the examples I am working from, they do n开发者_JAVA技巧ot look like expressions so I am very confused!

Please help!?


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.


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";


In ASPX page, @ symbol is used along with the page directives.

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

In the Code page, @ Symbol is used for the string values escape characters.

String s = @"c:/Document/Files/Sample.txt"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜