Results 1 to 6 of 6

Thread: What does the @ symbol mean in a string declaration?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    What does the @ symbol mean in a string declaration?

    What does the @ symbol mean in the following decleration?

    string keyName = @"MyKey";
    Brandon S Davids

  2. #2
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    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..

  3. #3
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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";
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  4. #4
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: What does the @ symbol mean in a string declaration?

    Quote 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

  5. #5
    Hyperactive Member Rattlerr's Avatar
    Join Date
    Jul 2005
    Location
    FloralCity,Florida
    Posts
    269

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    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.
    Brandon S Davids

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width