|
-
Jul 13th, 2006, 05:36 PM
#1
Thread Starter
Addicted Member
What does the @ symbol mean in a string declaration?
What does the @ symbol mean in the following decleration?
string keyName = @"MyKey";
-
Jul 13th, 2006, 05:52 PM
#2
Hyperactive Member
Re: What does the @ symbol mean in a string declaration?
Well assuming your using C# with ASP the @ symbol is a Page Directive...Example:
<%@ Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
their are Several @ Page directive that is used to define the managed language used within the page(via the language attribute).It may also define the name of the related code-behind file (if any),enable tracing support, and so forth..
-
Jul 13th, 2006, 06:28 PM
#3
Re: What does the @ symbol mean in a string declaration?
It means that it's a verbatim string literal - any escape characters are interpreted literally.
e.g., string test = @"a\b";
results in the backslash being interpreted literally as a backslash and not the usual C# escape character.
The only escape character in verbatim string literals is the double quote - it's used to escape a double quote.
A benefit of verbatim string literals is that you can construct a multi-line string literal easily:
string test = @"this is on the first line
and this is on the second line";
-
Jul 13th, 2006, 06:28 PM
#4
Hyperactive Member
Re: What does the @ symbol mean in a string declaration?
 Originally Posted by brandoom
What does the @ symbol mean in the following decleration?
string keyName = @"MyKey";
No, it's not a page directive at all here.
To understand the reason why the @ is there you should try the following:
Code:
Console.Writeline("c:\test");
Console.Writeline(@"c:\test");
You will probably discover the difference very fast.
The thing is that without the @-sign a string can contain certain codes:
Code:
\n is new line
\t is a tab
\\ is a \-sign
In C++ there was no such thing like the @-sign. Therefore hardcoded filepaths looked like this.:
Code:
string str = "c:\\program files\\someprogram\\somefile.txt";
you could also work like this in C#. But instead of putting every \ twice you can just use the @-sign in front of your string. It saves some time and effort.
Code:
string str = @"c:\program files\someprogram\somefile.txt";
I hope this explanation is clear.
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Jul 13th, 2006, 06:47 PM
#5
Hyperactive Member
Re: What does the @ symbol mean in a string declaration?
Yeah the @ symbol has alot of meanings...Even in the ASP like i mentioned its a Page Directive..Had my head in ASP to long..lol
-
Jul 13th, 2006, 07:00 PM
#6
Thread Starter
Addicted Member
Re: What does the @ symbol mean in a string declaration?
Thanks guys. It was in some registry code, so I can see why they were using the @ symbol. The funny thing was that they weren't using any backslashes in the strings. If they would have put one in, I would have figured it out.
Thanks again for chiming in.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|