At or @ usage

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

    At or @ usage

    I'm new to C#. What's the purpose of the @ ??? I can't find it
    indexed in my few C# books. It seems to have something to do with
    reading strings literally. That's just a guess. I notice if I place
    it outside a path that is in quotes, I don't need the extra backslash.
    For Example:

    string stagger=@"c:\no teq.cmd";

    is the same as

    string stagger="c:\\no teq.cmd";

    string stagger="c:\not eq.cmd";

    without the @ gives an error.
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: At or @ usage

    folderann wrote:
    I'm new to C#. What's the purpose of the @ ??? I can't find it
    indexed in my few C# books. It seems to have something to do with
    reading strings literally. That's just a guess. I notice if I place
    it outside a path that is in quotes, I don't need the extra backslash.
    For Example:
    >
    string stagger=@"c:\no teq.cmd";
    >
    is the same as
    >
    string stagger="c:\\no teq.cmd";
    >
    string stagger="c:\not eq.cmd";
    >
    without the @ gives an error.
    In this context @ means that \ is not escape character.

    So you avoid writing the two backslashes to get one.

    (you put a " in the text by doubling and you can have literal
    newlines in the string also)

    Arne

    Comment

    • Ken Foskey

      #3
      Re: At or @ usage

      On Wed, 06 Aug 2008 22:42:05 -0400, folderann wrote:
      I'm new to C#. What's the purpose of the @ ??? I can't find it indexed


      verbatim-string-literal:
      @" verbatim -string-literal-charactersopt "

      It removes the significance of the \ character, eg \n is a new line, \t
      is a tab.

      Comment

      • Marc Gravell

        #4
        Re: At or @ usage

        Just for completeness (from the post title, not the example given),
        note that an @ prefix is also used (separately) to allow you to use an
        otherwise reserved word (/keyword) as a regular name in C#. This is
        usually only used when dealing with cross-language issues (different
        languages have different keywords) - for example, you can refer to a
        member/type/variable @default, when without the @ it would get
        interpreted as a language keyword.

        Marc

        Comment

        • cfps.Christian

          #5
          Re: At or @ usage

          I use it to create long strings such as SQL queries.
          Rather than concatenating the string together (at high cost) you can
          use the @ to create a multi-line string.

          string query = "SELECT * "
          + "FROM RandomTable";
          or

          string query = @"SELECT *
          FROM RandomTable ";

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: At or @ usage

            On Aug 7, 3:06 pm, "cfps.Christian " <ge0193...@otc. eduwrote:
            I use it to create long strings such as SQL queries.
            Rather than concatenating the string together (at high cost) you can
            use the @ to create a multi-line string.
            >
            string query = "SELECT * "
                 + "FROM RandomTable";
            or
            >
            string query = @"SELECT *
                 FROM RandomTable ";
            There is no execution time cost to doing this in the first form. So
            long as the strings are constant, they are concatenated by the
            compiler rather than at execution time.
            Even if it were performed at execution time, the cost of concatenating
            strings is going to be just noise compared with the cost of actually
            executing the query and waiting for the results.

            Now there's a reasonable argument that the second form is easier to
            edit etc, and that's all fine - but the performance argument is a
            nonstarter.

            Jon

            Comment

            • folderann

              #7
              Re: At or @ usage

              A belated thanks to all. It was very enlightening. I deduced that
              the @ took care of the identification of the escape sequence, but I
              didn't understand any of it's other uses.

              Thanks to all again. I got wrapped up in an other problema and forgot
              about this post.

              On Thu, 7 Aug 2008 07:15:47 -0700 (PDT), "Jon Skeet [C# MVP]"
              <skeet@pobox.co mwrote:
              >On Aug 7, 3:06Êpm, "cfps.Christian " <ge0193...@otc. eduwrote:
              >I use it to create long strings such as SQL queries.
              >Rather than concatenating the string together (at high cost) you can
              >use the @ to create a multi-line string.
              >>
              >string query = "SELECT * "
              >Ê Ê Ê+ "FROM RandomTable";
              >or
              >>
              >string query = @"SELECT *
              >Ê Ê ÊFROM RandomTable ";
              >
              >There is no execution time cost to doing this in the first form. So
              >long as the strings are constant, they are concatenated by the
              >compiler rather than at execution time.
              >Even if it were performed at execution time, the cost of concatenating
              >strings is going to be just noise compared with the cost of actually
              >executing the query and waiting for the results.
              >
              >Now there's a reasonable argument that the second form is easier to
              >edit etc, and that's all fine - but the performance argument is a
              >nonstarter.
              >
              >Jon

              Comment

              Working...