CSharp @

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

    CSharp @

    Hi,

    I know this isn't a C# group, but its just a quick one.

    As a seasoned C++ developer I'm learning C# as I go along. What's the @
    symbol for before a string?

    eg:

    FileInfo myFile = new FileInfo(@"c:\T emp\Test\readme .txt");

    Why can't it just be:

    FileInfo myFile = new FileInfo("c:\Te mp\Test\readme. txt"); ?

    Thanks

    David
  • David

    #2
    Re: CSharp @

    S'ok- found out.

    Its for treating the '\' marks as '\' marks, not leading characters.

    David wrote:
    Hi,
    >
    I know this isn't a C# group, but its just a quick one.
    >
    As a seasoned C++ developer I'm learning C# as I go along. What's the @
    symbol for before a string?
    >
    eg:
    >
    FileInfo myFile = new FileInfo(@"c:\T emp\Test\readme .txt");
    >
    Why can't it just be:
    >
    FileInfo myFile = new FileInfo("c:\Te mp\Test\readme. txt"); ?
    >
    Thanks
    >
    David

    Comment

    • Leon Mayne

      #3
      Re: CSharp @

      "David" <asd@asd.comwro te in message
      news:guWdnfIZ3t Oe05rVRVnyhwA@b rightview.com.. .
      Hi,
      >
      I know this isn't a C# group, but its just a quick one.
      >
      As a seasoned C++ developer I'm learning C# as I go along. What's the @
      symbol for before a string?
      >
      eg:
      >
      FileInfo myFile = new FileInfo(@"c:\T emp\Test\readme .txt");
      >
      Why can't it just be:
      >
      FileInfo myFile = new FileInfo("c:\Te mp\Test\readme. txt"); ?
      Because it escapes all the characters in the string. It couldn't be the
      second example you gave, it would have to be:

      FileInfo myFile = new FileInfo("c:\\T emp\\Test\\read me.txt");

      Comment

      • Mark Rae [MVP]

        #4
        Re: CSharp @

        "David" <asd@asd.comwro te in message
        news:guWdnfIZ3t Oe05rVRVnyhwA@b rightview.com.. .
        What's the @ symbol for before a string?
        Other than the regular string literals, C# supports what is called as Verbatim string literals.Verbatim string literals begin with @&quot; and end with the matching quote. They do not have escape sequences.



        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Cowboy \(Gregory A. Beamer\)

          #5
          Re: CSharp @

          It states the string is a literal, warts and all. Without the @, you are
          accepting \ as an escape character.

          Your second string would fail, as it is not escaping anything valid.

          --
          Gregory A. Beamer
          MVP, MCP: +I, SE, SD, DBA

          Subscribe to my blog


          or just read it:


          *************** *************** *************** ****
          | Think outside the box!
          |
          *************** *************** *************** ****
          "David" <asd@asd.comwro te in message
          news:guWdnfIZ3t Oe05rVRVnyhwA@b rightview.com.. .
          Hi,
          >
          I know this isn't a C# group, but its just a quick one.
          >
          As a seasoned C++ developer I'm learning C# as I go along. What's the @
          symbol for before a string?
          >
          eg:
          >
          FileInfo myFile = new FileInfo(@"c:\T emp\Test\readme .txt");
          >
          Why can't it just be:
          >
          FileInfo myFile = new FileInfo("c:\Te mp\Test\readme. txt"); ?
          >
          Thanks
          >
          David

          Comment

          Working...