tags:

views:

235

answers:

3
+1  Q: 

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

Please help!?

+3  A: 

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.

Keith
Thanks, I understand what you wrote, makes sense so +1 - but also, why is it that http://www.asp.net/learn/security/tutorial-08-vb.aspx (Scroll down to the bottom for last code example), almost every variable has an @ symbol, even when it doesn't look like they are declaring anything?
@noob2: please update your question to include a short example of what you're talking about.
John Saunders
In that think they're using `@` because SQL server does that before all parameters. I've updated my answer.
Keith
Wording: It won't throw an exception but a compiler error, right?
ssg
@noob2: did you notice that the example you're referring to is in VB.NET, not C#?
John Saunders
Yes, but it still answers the question fine - it is an asp.net issue and not really a c# or vb.net issue.
Got the rep so +1, thanks!
It's actually an ADO.NET issue -- and when adding parameters the `@` is optional in most cases (not in the T-SQL string).
RickNZ
+2  A: 

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"
solairaja
well, +1 when I have 15 reputation!
Got the rep, so +1, Thanks!
+3  A: 

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";
Oded
Got the rep, so +1, Thanks!