p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 (http://p2p.wrox.com/forumdisplay.php?f=409)
-   -   Ch 13 Use of @ in front of variable name (http://p2p.wrox.com/showthread.php?t=81760)

comecme November 30th, 2010 02:48 PM

Ch 13 Use of @ in front of variable name
 
I noticed that in listing 13-1, a @ is placed in front of buff and in front of txtStartingPath.Text.
Code:


string
startingPath = @startingPathTextBox.Text;
 
var myDrive = newDriveInfo(@buff); // Get its info


I wondered why. The only info I can find is that you can use @ in order to use a variable like int for instance, but that's not the case here. If it were, the declaration of the variable would've had the @ in front of it too.

DrPurdum November 30th, 2010 03:22 PM

The @ operator
 
Hi Comecme:

If you look way back on page 122, you'll see that the amperand operator (@) is used when you wish to use string literals in a verbatim manner. As you know, the backslash can be used as a "lead-in" character for special escape sequences, like "\n" for a newline character. If you place the ampersand before the string variable on the righthand side of an assignment, that means you want a verbatim translation of the string. A common example is a directory path like: "C:\Windows\System". If you don't use the ampersand character, the same string would have to be written" "C:\\Windows\\System". Reread pages 122-123 and I think you'll see what I mean.

comecme November 30th, 2010 05:09 PM

Hi DrPurdum, thanks for the quick reply.

I know you can use the @ for verbatim string literals. But in this code you are using verbatim variable names. I've tried the code without the @ and it doesn't seem to make any difference.
Code:

// Verbatim string literal
string strVerbatim = @"C:\Temp";
 
// Verbatim variable named int
int @int = 1;
 
// Part of code listing 13-1
string startingPath = @startingPathTextBox.Text;
var myDrive = new DriveInfo(@buff);

In your code, if you'd be using string literals, both @'s would make sense.

PS: Isn't the ampersand operator the & and the at sign @?

DrPurdum December 2nd, 2010 09:52 AM

@ Operator
 
First, you're right. The "@" operator is the "at" sign while the "&" is the amperand.

As to your code, while your statement:

int @int = 1;

is legal, it's a really bad idea to use a keyword as the basis for a variable name, especially when using a legal operator as the first letter for the name. (Indeed, I think it should be an error to use the @ in this manner.) If you want to reflect the idea that you're using an integer, I would change this to:

int myInteger = 1;

or something like that. Even better would be a name that reflects the purpose of the variable.

The string literal operator (@) would have no effect on the variable you defined above since it only appears on the left side of the assignment operator. In that case, it is simply viewed as another "letter" in the identifier's name (e.g., @int). This means it is used in the expression in such a way that the compiler does not interpret it as operator.

In the case of the first statement:

string strVerbatim = @"C:\Temp";

the compiler sees the operator on the righthand side of the assignment expression and says: "Aha! The backslash is a literal and means I should NOT interpret the "\T" as the tab character."

Finally, the @buff used as the argument to DriveInfo() causes the compiler to alter the attribute list for buff in the symbol table to accept any string data as a string literal. Remember that arguments passed to a method are passed on the stack and "popped" off into a temporary variable in the method. (Reference variables pop the lvalue instead of the rvalue.) This means that the method arguments are "assigned" to the temporary variables created by the method. Therefore, the @buff argument can be viewed as executing the statement:

string strTemp = @buff;

inside the DriveInfo() method.

I hope this helps.

comecme December 2nd, 2010 11:58 AM

I think I'm not making myself clear. Maybe that's because I'm Dutch? [;)]

First of all, in the example with @int, the at sign is not just another character in the variable name. The @int is a verbatim identifier. This means it makes it possible to create a variable named int, even though int is a keyword in C#. Since the @ isn't part of the variable name itself, buff and @buff in your code would refer to the same variable (the same lvalue as well as the same rvalue).

But verbatim identifiers are not what my question's about. My question is why you use the at sign in the @buff and @startingPath.Text on the righthand side of the operator. Like I mentioned before, I see no difference in the process step of the program if I leave out the at sign. If you type a "\" in the textbox, the Text property will show you "\\". If you'd type in a tab character, the Text property would show "\t", but if you type "\t" it would show "\\t".

If you try this
Code:

string startingPath = startingPathTextBox.Text;
string startingPath2 = @startingPathTextBox.Text;
if (startingPath.Equals(startingPath2))
{
  MessageBox.Show("They are the same!");
}

you'll see the strings are the same.

Either with or without the @, the variable startingpath will contain "C:\\Temp" after I've typed in C:\Temp in the textbox. For the @buff I see no difference either. And by not seeing any difference, I mean I've checked the value of startingPath in the debugger.

DrPurdum December 2nd, 2010 04:37 PM

@ Operator
 
We're probably talking at cross purposes.

First, I was just wondering why you would use the statement

int @int = 1;

What did you have in mind? I can't imagine a good reason for such code.

Second, the @ operator is really used for string literals, or quoted strings, in the code. Look at the following statements in the debugger and it may become clear what the operator does:

string startPath1 = @"C:\\temp";
string startPath2 = "C:\\temp"; ;

The result is that startPath1 ends up looking like "C:\\\\temp" while startPath2 has "C:\\temp". In the first case, the literals causes the compiler to view the backslashes as escape sequences, so it translates the literal. The @ operator simply saves the programmer some typing when path names are hard coded. Without the @ operator, what you see is what you get.

comecme December 2nd, 2010 06:42 PM

I have no idea why someone might want to use @int as a variable name, I was just showing that the at sign in front of a variable name is used to make that possible. Let's not talk about @int any more.

The use of the at sign in front of a string literal is clear to me. @"C:\Temp" is the same as "C:\\Temp".

But why do you use the at sign in method btnList_Click of listing 13-1 in the book?
Code:

startingPath = @txtStartingPath.Text;
and in ShowDriveInfo?
Code:

DriveInfo myDrive = new DriveInfo(@buff);
You are the one who started using @ in front of a variable name, not me. [:p]
In these lines of code, we are not talking about a verbatim string literal. There are no string literals in this code!

Again, if I remove both @'s in listing 13-1, I get the exact same results. So, is there any reason to use the @ in these two lines of code?

Old Pedant December 2nd, 2010 07:23 PM

Dr.Purdum wrote:
Quote:

Finally, the @buff used as the argument to DriveInfo() causes the compiler to alter the attribute list for buff in the symbol table to accept any string data as a string literal.
*IF* I understand what Dr.Purdum was saying, it would mean that doing
Code:

DriveInfo myDrive = new DriveInfo(@buff);
would imply that if buff had "c:\temp" assigned to it then it would be taken literally, and \t would not mean TAB.

But that's NUTSO!!!

What if you had
Code:

String foo = "c:\temp";
in some completely separate module, some separately compiled code. You and I (and Dr. Purdum) know that OF COURSE that will be seen as the string
Code:

    c:[tab]temp
And then suppose you did
Code:

String buff = foo;
DriveInfo myDrive = new DriveInfo(@buff);

Would that imply that somehow the runtime would REINTERPRET the string from foo so that the [tab] magically is reconverted to two characters "\" and "t"????

I don't believe it.

comecme, I'm with you. None of this usage makes sense to me.


All times are GMT -4. The time now is 06:40 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.