C # Beginners need to be aware of the details (in continuous update)

Source: Internet
Author: User
Tags case statement goto rar tostring
Beginner

Recently had the opportunity to speak C #, beginners easy to make some mistakes, perhaps we these old birds usually will also ignore. If you have time, you will tidy up some.

1. @ In use:

In many languages, the @ is often referred to as the address before the variable, and @ in C # is used in front of the string variable, which means ignoring all the escape characters "\" in the string. Such as:

Console.writeln ("C:\\downloads\\test.rar");

Equivalent to the following statement:

Console.writeln (@ "C:\downloads\test.rar");

2. Differences between convert.to* and var.to* usage

All of the above two functions are explicitly variable type conversions, which are generally used for variables that cannot be implicitly converted, using the following examples to notice their differences.

int intext = 1999;
string sttext = "";

Sttext = convert.tostring (intext); //Convert syntax examples

Sttext = Intext.tostring (); //Note that the variable is directly to the new type of usage, the latter () is not to be saved, which is different from some languages

Console.WriteLine ("This is convert test string:" + Sttext);

3. The difference between C # 's switch and C + +

Grammar:

Switch(VAR)
{
CaseVarvalue1:
< statement group >
Break;
CaseVarvalue2:
< statement group >
Break;
......
CaseVarvaluen:
< statement group >
Break;
default://default can be omitted
< statement group >
Break;

}

C # and C + + have the same switch syntax, but C + + allows the case statement to be run down, that is, when the break is omitted, the following set of box statements is continued, and the usage is prohibited in C #. In C # 's switch statement, the break is not small, but you can skip it in the way of Goto case Varvalue and execute the following case statement group directly. Examples are as follows (perhaps the example is not logical, just to show how C # is running through the switch):

Console.WriteLine ("This is convert test string:" + Sttext);

int var1 = Convert.ToInt32 (Console.ReadLine ());

Switch (VAR1)
{
Case 70:

Console.WriteLine ("The number is!\n");
Goto Case 80; //Take case 80 as a goto label
break;

Case 80:

Console.WriteLine ("The number is!\n", var1);
goto default;
break;

default:

Console.WriteLine ("The number is not" or "!\n");
break;

}



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.