Beginner C # variables, placeholders, escape characters, and type conversions

Source: Internet
Author: User

㈠, defining variables
    • Define the re-assignment first
int  Num1; Num1  = ten;
    • Assigning values at the same time as defined
int Age = ;
    • Define multiple variables to be assigned at the same time, the prerequisite variable type is the same, for example:
string Phome = "18912508888 "  " Wanda ";
    • When defining a variable, the data type is added after the "? ”

After the variable type, add "? "Represents a nullable value type. For example:

int null; // error Hint: cannot convert null to ' int ' because it is a non-nullable value type

But the following format is not an error.

int=null;
㈡ "+" and placeholder {} in string
    • is the addition operation in mathematics: The data type of the two changes participating in the addition operation is the numeric type, then "+" is the meaning of the mathematical addition.
    • is connected to the meaning: the two sides participate in the plus operation of the data, there is a character type, then the "+" is the meaning of the connection.

Placeholder is the first place to occupy a fixed position, waiting for you to add the contents of the symbol. The position character is made up of {number} and the number is numbered starting with 0.

1th placeholder: {0}

2nd placeholder: {1}

2nd placeholder: {2}

string " Zhang San " ; int  - ; decimal salary = 7600m; // The placeholder {0}{1}{2} has no sequential requirements, and you can reuse {0} in a sentence to represent the first parameter. // emphasis: The number of placeholders cannot exceed the total number of parameters minus 1. Over error Console.WriteLine (" my name {0}, this year {1} years old, my salary {2} yuan ", name, age, salary);
The role of the ㈢ escape character and the "@" symbol in a string

Commonly used escape characters and their meanings:
\ ' Single quotation mark
\ "Double quotation marks
\ \ counter Slash

Console.WriteLine ("e:\\program files\\ fire horse game \\GamePlaza.exe");


Air
\a warning (generates peak)
\b Backspace //\b is the backspace function on the keyboard, but adds no effect at the end of the character channeling.
\f Page Change
\ nthe line break

Console.WriteLine ("Boiled beans, green beans in the kettle, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \")


\ r Enter //Add the character after the \ R in the string to overwrite the preceding character

\ t Horizontal tab //Is the keyboard's tab effect
\v Vertical Tab

The function of the @ symbol:

    • The escape function of the escape symbol "\" in the string is canceled.

    • Output a string in the original format
Console.WriteLine (@ "E:\Program files\ fire horse game \gameplaza.exe");

㈣ data type implicit conversions

The so-called implicit conversion, which is the system default conversion, is essentially a small storage capacity data type that is automatically converted to a large storage capacity data type.

For example: Int (implicit conversion) ==> long , float , double ordecimal

Implicit numeric conversion tables (C # Reference)

You guess the result of the following operation:

int Ten 3 ; double quo = A/b; // Results: 3

Originally I thought is: 3.333333. , but in fact it turns out to be 3. The reason is that the operands (a, b) of the participating operations are all the same type, and the result type is the same as the type of the operand. Then the assignment to the quo variable becomes a double type.

Where the operand has a double type, the result of the operation is a double type. As follows:

int Ten 3 ; Double 1.0 // result 3.3333 ... 
㈣ Data type display conversion

Method 1. This conversion is primarily used for conversions between numeric types, and implicit conversions can be used from the int type to the Long,float,double,decimal type conversion, but a display conversion is required from long to int, which means that a compilation error is generated by using that type of conversion.

This way for floating-point numbers will be unconditionally, will lose precision

For conversions of type char to int, the value returned is an ASCII code

 double  pi = 3.1415926  ;  int  r = 5  ;  double  s = pi * Math.pow (R, 2 ); //  result 78.539815  int             temp = (int ) s; //  result 78. The number of decimal parts  char  sex =  "  n   " ;temp  = ( int )             Sex //  result 110, you can convert single-letter  

Method 2. Int. Parse (string variable name) This method converts a string of numeric content to an int type, or throws a ArgumentNullException exception if the contents of the string are null, or if the string content is not a number, The FormatException exception is thrown.

Use this method to handle only the contents of a string, and the converted string content is within the range of the int type that can be represented

stringSTR1 ="3.1415926";DoubleDbla =Double. Parse (STR1);//results: 3.1415926//int Num1 = Int. Parse (STR1); //Grammar did not error, start the times wrong. The hint string is malformed. Cause int.parse can only be turned to integer numeric stringintNUM1 = (int)Double. Parse (STR1);//results: 3 in two-step conversion is possible.Console.WriteLine ("Dbla = {0} Num1 = {1}", DBLA,NUM1)

Method 3. Convert.ToInt32 This method not only converts the string type to int, but also converts the other type to int. If the variable is an object or string type, when its value is nul, it returns 0, without causing a program error, but if the value of this string type is string. Empty, the program will still get an error when converting to int.

string " 3.1415926 " ; double Dbla = convert.todouble (STR1); // results: 3.1415926

Beginner C # variables, placeholders, escape characters, and type conversions

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.