C # (3) variables

Source: Internet
Author: User
5. More information about variables

5.11 Implicit conversion Generally, the data type with a small number of bytes is converted to a data type with a large number of bytes (of course, it can be equal ). For example: Ushort And Char (Equal byte) Int And Double (small-> large)
5.12 Explicit conversions
Explicit conversions generally convert a type with a large number of bytes to a type with a small number of bytes.
Example: byte destinationbyte;
Short sourceshort

Destinationbyte = (byte) sourceshort;
Note: In this case, the value assignment may cause result overflow;
Example: sourceshort = 281;
If destinationbyte = (byte) sourceshort is executed, the value of destinationbyte will be changed to 25.
Because 281 = 100011001
25 = 00011001
255 = 01111111
At this time, we can take appropriate measures to solve the problem.
For example: checked (expression)
Or unchecked (expression)
Destinationbyte = checked (byte) sourceshort)
Another method is to modify the configuration application. Program :
① Click solution Resource Manager.

Set

Click "advanced" on the right and click "operate ".

5.1.3 use the convert command for explicit conversion

Int varint;
Short varshort = 10;
Float varfloat = 20;
Varint = varshort * (short) varfloat; // The result of this sentence is a value of the int type. If varint is of another type, an error is returned.
Vardouble = convert. todouble ("125 ");
Console. writeline (varint );
5.2Complex Variable types ① Enumeration type Form: Enum typename {value1 , Value2 , Value3 , .. Valuen } This type uses one Int Type Storage If you want to change the storage, you can change the definition: Enum typename : Underlyingtype( Int ) {Value1 , Value2 , Value3 , .. Valuen } Frequent outlets Enum typename {value1 = actualval , Value2 , Value3 = value1 , Value4 , . Valuen }In this case Value2 = value4 This kind of detour may easily lead to errors. Enum {value1 = value2value2 = value1} The enumeration type definition is generally placed in the namespace. Namespace test { Enum direction { North, South, East, West } Class Program { } Structure ② Struct typename {} ③ Array declaration: (1) One-dimensional array: Definition: for example: Int [] varint; Initialization: Const int varint = 5 ; Int [] arrary = {5, 3, 9, 4, 3}; int [] arrary = new int [5]; By default, each element is 0. Int [] arrary = new int [varint]; By default, each element is 0. It cannot be written like this during definition : Int [] array; Array = new int [5]; Because the array size must be allocated when it is defined. When assigning an initial value to an array, note that the array size must match the number of elements. For example: Int [] array = new int [5] {1, 2, 3, 4, 5 }; Int [] array = new int [5] {1, 2, 3 }; This is wrong Yes Foreach Loop to read the value of the array element, but cannot be modified. In contrast, For You can read and modify information in a loop. Foreach (INT VaR in array) { Console. writeline (VAR ); } (2) Multi-dimensional array Definition: Double [,] hillheight = new double [3, 4]; Initial values: Double [,] hillheight = new double [2, 3] {4, 2, 3}, {4, 5, 6 }}; Or Double [,] hillheight = {, 3}, {, 6 }}; (3) Array (the number of elements in each row can be different. Strictly speaking, it is a type of multi-dimensional array. ) Initial Values: ① Int [] [] var2; Var2 = new int [2] []; Var2 [0] = new int [4]; Var2 [1] = new int [3]; Var2 [0] [1] = 2 ;// This is different from multi-dimensional arrays because Var2 [1, 2] ② Int [] [] var2 = {New int [1] {2}, new int [2] {3, 4 }}; ③ Int [] [] var2; Var2 = new int [2] [] {New int [1] {2}, new int [2] {3, 4 }}; Also available Foreach Read the value of the array by using nested Foreach () foreach (INT [] vararray in var2) { Foreach (INT var3 in vararray) { Console. writeline (var3 ); } } 5.5 String processing ① String String Can be converted Char Array For example: String mystring = "a string "; Char [] mychar = mystring. tochararray (); Foreach (char CH in mychar) { Console. writeline (CH ); } Console. writeline (mychar. Length );// The size of the output array. ② Convert string to lowercase For example: String userresponse = console. Readline (); If (userresponse. tolower () = "wtq ") { Console. writeline (" You are great ");} ③ Convert string to uppercase For example: String userresponse = console. Readline ();If (userresponse. toupper () = "wtq ") { Console. writeline (" You are great "); } ④ Delete extra spaces For example : String userresponse = console. Readline (); Userresponse = userresponse. Trim (); If (userresponse. toupper () = "wtq ") { Console. writeline (" You are great "); } ⑤ Delete additional characters For example: Char [] trimchars = {'', 't', 'q '}; String userresponse = console. Readline (); Userresponse = userresponse. Trim (trimchars ); If (userresponse = "W ") { Console. writeline (" You are great "); } ⑥ Delete the leading or trailing spaces of a string <String>. trimstart (), <string>. trimend For example: String userresponse = console. Readline (); Userresponse = userresponse. trimstart (); If (userresponse = "W ") { Console. writeline (" You are great "); } 7. <String>. padleft And <String>. padright Add spaces on the left or right of the string. For example: String mystring = "aligned ";Mystring = mystring. padleft (10 ); Console. writeline (mystring ); } Note: You can also add other characters before the string. For example: String mystring = "aligned "; Mystring = mystring. padleft (10, '+ '); Console. writeline (mystring ); Delimiter <String>. Split () For example: String mystring = "this is a test "; Char [] separator = {'', 'I '}; String [] mywords; Mywords = mystring. Split (separator ); Foreach (string word in mywords) { Console. writeline (Word ); } Reverse returns the string in reverse order. Method 1: String text = "wtqwtqwtq "; Char [] carray = text. tochararray ();
String reverse = string. Empty ;// Equivalent Reverse Initial Value assignment
For (INT I = carray. Length-1; I>-1; I --)
{
Reverse + = carray [I];
} Console. writeline (reverse ); Method 2: String text = "wtqwtqwtq "; Char [] chararray = text. tochararray (); Array. Reverse (chararray ); Console. writeline (chararray );

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.