Use of the @ symbol in C #

Source: Internet
Author: User
Tags integer join

The @ symbol in C # is actually a lot of usage, so let's look at what's amazing about @.

1. Qualifying string

Precede the string with the @ symbol to indicate that the escape character "No" is being processed.

If we write a path to a file, such as a text.txt file under the "d:\ text file" path, the words are written as follows without the @ symbol:

String fileName = "d:\\ text file \\text.txt"; If you use the @ symbol, it's easier:

String fileName = @ "d:\ text file \text.txt"; 2. Let string Cross Line

Sometimes a string written in a line can be very long (such as SQL statements), not using the @ symbol, a writing is this:

string strSQL = "SELECT * FROM HumanResources.Employee AS e"
+ " INNER JOIN Person.Contact AS c"
+ " ON e.ContactID = c.ContactID"
+ " ORDER BY c.LastName";

With the @ symbol, you can change the line directly:

string strSQL = @"SELECT * FROM HumanResources.Employee AS e
INNER JOIN Person.Contact AS c
ON e.ContactID = c.ContactID
ORDER BY c.LastName";

3. Usage in identifiers

C # does not allow keywords to be used as identifiers (class names, variable names, method names, table space names, and so on), but if you add @ then you can, for example:

Namespace @namespace
... {
    class @class
    ... {public
        static void @static (int @int)
        ... {
            if (@int > 0)
            ... {
                System.Console.WriteLine ("Positive Integer");
            }
            else if (@int = 0)
            ... {
                System.Console.WriteLine ("Zero");
            }
            Else
            ... {
                System.Console.WriteLine ("Negative Integer");}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.