Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net @ Symbol

Tags:

asp.net

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 not look like expressions so I am very confused!

Please help!?

like image 854
user232052 Avatar asked Dec 15 '09 11:12

user232052


1 Answers

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.

like image 194
Keith Avatar answered Oct 20 '22 13:10

Keith