@ sign?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben R.

    @ sign?

    I saw the following line of sourcecode:
    string input = @"the quick brown fox jumped over the lazy dog";

    What is the purpose of the '@' above? I tried running with and without and
    get seemingly same results. Anyone know?
  • Dan

    #2
    Re: @ sign?

    That string will be the same with or without the '@'. The '@'
    eliminates escape chars by telling the compiler that it is a verbatim
    string. This makes writing stuff like file paths easier.

    @"c:\stuff.t xt" represents just what it says, however "c:\stuff.t xt"
    will give a compile error because it is trying to find a special char
    related to \s.

    MessageBox.Show ("Hello\nWorld" )
    gives:

    Hello
    World

    MessageBox.Show (@"Hello\nWorld ")
    gives:

    Hello\nWorld

    I would not suggest using it when it is not needed.

    HTH,
    Dan

    Comment

    • L

      #3
      Re: @ sign?

      @ is used when you want to assign a file path to a string.

      ex:

      string strFilePath = @"C:/MyDir/thisFile.txt"

      if you are not using @, you got to assign it as

      string strFilePath = "C://MyDir//thisFile.txt"

      -Lalasa.

      Comment

      • Dan

        #4
        Re: @ sign?

        This is true because "\\" is a special character interpreted as literal
        "\".

        Another very important use is when dealing with regex. You want regex
        to interpret "\w" as a word character, not make the compiler puke
        trying to figure out what it means. You don't want to write confusing
        and ugly regex statements where all of the "\" characters are doubled.

        HTH,
        Dan

        Comment

        • MarkT [developmentor]

          #5
          RE: @ sign?

          It also lets you embed a return in a string, for example:

          string stuff = @"hello
          there, how are
          you?";

          Comment

          • Christian T.

            #6
            RE: @ sign?

            That is a quoted string literal.

            The advantage of @-quoting is that escape sequences are not processed,
            which makes it easy to write, for example, a fully qualified file name:
            @"c:\Docs\Sourc e\a.txt" // rather than "c:\\Docs\\Sour ce\\a.txt"
            (http://msdn.microsoft.com/library/de...-us/csref/html
            /vclrfString.asp )

            The reason you don't see a difference in your example is because your
            string does not have any escape sequences.

            Cheers,
            Christian T. [MSFT]
            Visual Studio Update Team

            - Please do not reply to this email directly. This email is for newsgroup
            purposes only.
            =============== =============== =============== =============== =============
            This posting is provided "AS IS" with no warranties, and confers no
            rights. Use of included script samples are subject to the terms specified
            at http://www.microsoft.com/info/cpyright.htm

            Note: For the benefit of the community-at-large, all responses to this
            message are best directed to the newsgroup/thread from which
            they originated.
            =============== =============== =============== =============== =============

            --------------------[color=blue]
            >Thread-Topic: @ sign?
            >thread-index: AcU2NQb+T1FGqU7 RREWhEgddymJE2Q ==
            >X-WBNR-Posting-Host: 129.33.1.37
            >From: =?Utf-8?B?QmVuIFIu?= <BenR@discussio ns.microsoft.co m>
            >Subject: @ sign?
            >Date: Thu, 31 Mar 2005 13:03:02 -0800
            >Lines: 5
            >Message-ID: <83C06EB2-3089-499C-BC3A-C6D26447314F@mi crosoft.com>
            >MIME-Version: 1.0
            >Content-Type: text/plain;
            > charset="Utf-8"
            >Content-Transfer-Encoding: 7bit
            >X-Newsreader: Microsoft CDO for Windows 2000
            >Content-Class: urn:content-classes:message
            >Importance: normal
            >Priority: normal
            >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
            >Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
            >Path: TK2MSFTNGXA03.p hx.gbl
            >Xref: TK2MSFTNGXA03.p hx.gbl microsoft.publi c.dotnet.langua ges.csharp:3170 05
            >NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.1.29
            >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
            >
            >I saw the following line of sourcecode:
            >string input = @"the quick brown fox jumped over the lazy dog";
            >
            >What is the purpose of the '@' above? I tried running with and without and
            >get seemingly same results. Anyone know?
            >[/color]

            Comment

            • Ted Miller

              #7
              Re: @ sign?

              It also allows you to use reserved words as variable names.

              public void Foo(string @string)
              {
              // ...
              }


              "Dan" <danielshanyfel t@yahoo.com> wrote in message
              news:1112303915 .100741.97460@z 14g2000cwz.goog legroups.com...[color=blue]
              > That string will be the same with or without the '@'. The '@'
              > eliminates escape chars by telling the compiler that it is a verbatim
              > string. This makes writing stuff like file paths easier.
              >
              > @"c:\stuff.t xt" represents just what it says, however "c:\stuff.t xt"
              > will give a compile error because it is trying to find a special char
              > related to \s.
              >
              > MessageBox.Show ("Hello\nWorld" )
              > gives:
              >
              > Hello
              > World
              >
              > MessageBox.Show (@"Hello\nWorld ")
              > gives:
              >
              > Hello\nWorld
              >
              > I would not suggest using it when it is not needed.
              >
              > HTH,
              > Dan
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                RE: @ sign?

                Christian T. (MSFT) <Christian_T.@o nline.microsoft .com> wrote:[color=blue]
                > That is a quoted string literal.[/color]

                Otherwise known as a verbatim string literal (e.g. by the C# spec :)

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Ben R.

                  #9
                  RE: @ sign?

                  Thanks to everyone who responded to this question. Your answers were all very
                  clear and helpful. Cheers...

                  -Ben

                  "Ben R." wrote:
                  [color=blue]
                  > I saw the following line of sourcecode:
                  > string input = @"the quick brown fox jumped over the lazy dog";
                  >
                  > What is the purpose of the '@' above? I tried running with and without and
                  > get seemingly same results. Anyone know?[/color]

                  Comment

                  Working...