The @ symbol

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • knowthediff@gmail.com

    The @ symbol

    hello,

    What does the "@" symbol do in the following line of code? I tried
    searching the web but searching for "@ and c#" seems to be a waste of
    time :)

    FileInfo fi = new FileInfo(di.Ful lName + @"\myxmlfile.xm l");

    Thanks,
    -J

  • Mattias Sjögren

    #2
    Re: The @ symbol

    >FileInfo fi = new FileInfo(di.Ful lName + @"\myxmlfile.xm l");

    It makes it a verbatim string literal where \ isn't treated as an
    escape character. It's equivalent to the regular string
    "\\myxmlfile.xm l".

    You may also want to consider calling System.IO.Path. Combine instead
    of doing straight string concatenation.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Bruce Wood

      #3
      Re: The @ symbol

      It indicates that the string should be taken as-is: the backslash in
      this case is _not_ an escape character. It's meant to make writing
      Windows / DOS pathnames easier. Rather than having to write this:

      FileInfo fi = new FileInfo(di.Ful lName + "\\myxmlfile.xm l");

      so that the \\ resolves into a single \, you can specify no escapes:

      FileInfo fi = new FileInfo(di.Ful lName + @"\myxmlfile.xm l");

      so here the compiler doesn't attempt to resolve "\m" into a special
      character. This can be very handy if you're using UNC names:

      string myPath = @"\\unix1\tiffs \product\4x4\M0 356A.tif";

      instead of (yuck):

      string myPath = "\\\\unix1\\tif fs\\product\\4x 4\\M0356A.tif";

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: The @ symbol

        <knowthediff@gm ail.com> wrote:[color=blue]
        > What does the "@" symbol do in the following line of code? I tried
        > searching the web but searching for "@ and c#" seems to be a waste of
        > time :)
        >
        > FileInfo fi = new FileInfo(di.Ful lName + @"\myxmlfile.xm l");[/color]

        See http://www.pobox.com/~skeet/csharp/s....html#literals

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Brian Gideon

          #5
          Re: The @ symbol

          In addition to what others have said it can also be used on identifiers
          to allow language keywords as names. It's not something you see very
          often though. For example, a VB.NET developer could decide that the
          identifier "sealed" would be a good name for a property not knowing
          that it is a keyword in C#. A C# developer using the VB.NET code would
          need to qualify it with an @.

          Brian

          knowthediff@gma il.com wrote:[color=blue]
          > hello,
          >
          > What does the "@" symbol do in the following line of code? I tried
          > searching the web but searching for "@ and c#" seems to be a waste of
          > time :)
          >
          > FileInfo fi = new FileInfo(di.Ful lName + @"\myxmlfile.xm l");
          >
          > Thanks,
          > -J[/color]

          Comment

          • knowthediff@gmail.com

            #6
            Re: The @ symbol

            Thanks for the info :)

            Comment

            • knowthediff@gmail.com

              #7
              Re: The @ symbol

              Thanks for the info :)

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: The @ symbol

                Brian Gideon <briangideon@ya hoo.com> wrote:[color=blue]
                > In addition to what others have said it can also be used on identifiers
                > to allow language keywords as names. It's not something you see very
                > often though. For example, a VB.NET developer could decide that the
                > identifier "sealed" would be a good name for a property not knowing
                > that it is a keyword in C#. A C# developer using the VB.NET code would
                > need to qualify it with an @.[/color]

                Of course, anyone following the .NET naming conventions would
                automatically avoid all C# keywords because all keywords in C# are in
                lower-case, and all non-private members in the .NET naming conventions
                are PascalCased :)

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                Working...