C # collection Operators
1. String concatenation operator ("+ ")
The Character String concatenation operator connects two strings to form a new character string. It appears in the Program ("prompt character" + variable), which serves as a character connection.
Use an example to illustrate the functions of the String concatenation operator:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace operator {class Program {static void Main (string [] args) {int a = 26; int B = 10; int c; c = a + B; Console. writeLine ("the value of natural expression a + B is: {0}", a); // The output format Console in C. writeLine ("{0} + {1} = {2}", a, B, a + B); // Console of the output format of C. writeLine ("the value of natural expression a + B is:" + a); // here "+" serves as the character connection Console. writeLine ("Return Value Type of a + B: {0}", (a + B ). getType (); // display the Data Type of the returned value c string str1 = "This is"; string str2 = "a new string"; Console. writeLine (str1 + str2); // here "+" serves as a string connection Console. readLine ();}}}
The output result is:
2. is Operator
The is operator is used to dynamically check whether the object Runtime is compatible with the given type. The format is; expression is type. A boolean value is returned for the running result, indicating the type of "expression". if Europe can be converted by reference, boxing conversion or unpacking conversion (other conversions are not considered by the is operator), and then converted to the "type" to be judged ".
The following example describes the functions of operators:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace operator {class Program {static void Main (string [] args) {object a = 10; if (a is bool) {Console. writeLine ("B is a bool type");} else {Console. writeLine ("B is not a bool type");} Console. readLine ();}}}
The output result is: B is not a bool type.
3. as Operator
The as operator is used to convert a value explicitly (using reference conversion or boxing conversion, if other conversions are executed, these conversions should be performed for the forced conversion expression) to a given reference type. The format is as reference type. If the conversion specified by as cannot be implemented, the calculation result is null. You can use this to determine whether an expression is of a certain data type.
An example is provided to show whether each element in the array nums is of the string type:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace operator {class Program {static void Main (string [] args) {object [] nums = new object [3]; nums [0] = "123 "; nums [1] = 456; nums [2] = "string"; for (int I = 0; I <nums. length; I ++) // traverses all the elements of the array nums {string s = nums [I] as string; // converts the corresponding elements to the string Console. writeLine ("nums [{0}]:", I); if (s! = Null) {Console. writeLine ("'" + s + "'");} else {Console. writeLine ("not a string") ;}} Console. readLine ();}}}
The output result is:
4. When the relational operator is used to compare two characters, the program is;
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace operator {class Program {static void Main (string [] args) {bool b1; char char1 = 'C'; char char2 = 'D '; byte [] unicode1 = Encoding. unicode. getBytes (new char [] {char1}); // converts Unicode of character c to byte [] unicode2 = Encoding. unicode. getBytes (new char [] {char2}); Console. writeLine ("Unicode value of the character 'C': {0}", unicode1 [0]); Console. writeLine ("the Unicode value of the character 'D' is: {0}", unicode2 [0]); b1 = char1> char2; Console. writeLine ("char1> char2 value: {0}", b1); Console. readLine (); Console. readLine ();}}}
The output result is:
5. packing and unpacking in C #
To put it simply, the packing and unpacking in C # language are as follows:
Packing: Convert the value type to the reference type.
Binning: converts a reference type to a value type.