1.Null
// Features:
//1. Indicates a null reference that does not refer to any object, no memory space is created, and a null reference pointer is stored;
//2.Null type is System.Nullable
//3. For value types, the assignment cannot be directly null, but the reference type can
//4.Default value of null reference type variable
//int value type cannot be directly assigned to null;
//string reference type can be assigned null directly;
//int a = null;
//Example 1: Null can also be assigned to the int type
Nullable<int> a = null; //2. //float f = null; Nullable<float> f = null; Console.WriteLine(f); string b = "123"; b = null;
if (b == "123" || b == null) { Console.WriteLine(1); } Console.WriteLine(a); Console.WriteLine(b);
//The difference between null and string null value and string space
//1.null means no memory space is created,
//2. A string null value of “” or string.Empty will actually allocate space;
//3. String spaces ” ” will allocate space. Spaces are also symbols corresponding to ACSII.
Example: Determination of input account format:
string strTest2 = null; string strTest3 = ""; string strTest31 = string.Empty; string strTest4 = " "; Console.WriteLine("Please enter your account number:"); string strTest = Console.ReadLine(); if (strTest == null || strTest == string.Empty || strTest.Trim() == "") { Console.WriteLine("Account format error!!"); } else { string newStr = strTest.Trim(); Console.WriteLine(newStr); }
————————————————– ————————————————– —————————-
2.Double question mark? ? (coalescing operator)
// Function:
//Nullable type is used when assigning values to non-nullable types.
//Used to determine if a variable is null and return a specified value
//scenes to be used:
//The Null coalescing operator defines a default value for type conversion,
//In case the value of a nullable type is Null
example:
// ? nullable type int? tempIntA = null; //tempIntA = 10; int tempIntB; // If tempIntA is null, tempIntB takes the value 100. Otherwise, takes tempIntA. tempIntB = tempIntA 100; Console.WriteLine(tempIntB); // 100 string tempStr = null; tempStr = "456"; // Reference types can also be used, which is equivalent to an if statement string tempStr2; tempStr2 = tempStr "10086"; Console.WriteLine(tempStr2); tempStr2 = tempStr == null ? "12306" : tempStr; Console.WriteLine(tempStr2);
3. Immutable array (one-dimensional array)
// Meaning:
// An immutable array is a fixed-size sequential collection that stores elements of the same type.
// Features:
//The array is a reference type
// Arrays can only contain elements of the same type
// The array is marked with subscript (index value) elements
Format:
Define array:
datatype[] arrayName;
*/
// int integer type
//int[] array type
example:
//Array int aa = 1; int[] aarry = { aa, 2, 3, 4 }; // Loop through the output array elements foreach (int i in aarry) { Console.WriteLine(i); }
How to initialize elements in an array:
Data type [] array name = new data type [length]; Data type [] array name = {value 1, value 2, …} Data type [] array name = new data type [length] {value 1, value 2,…} Data type [] array name = new data type [length can be ignored] {value 1, value 2,…}
examples;
int s = 1; //int[] array type can expand the array length during initialization int[] intSum = { 1, 2, 3, 5, 4, 6, 7 }; // During initialization, determine the length of the array. If written, it must be filled! ! ! //The content length of the array must be consistent with the length of the array float[] floatArray = new float[4] { 10.2f, 56.8f, 28.6f, 3.6f }; // During initialization, the length of the array is uncertain. The number of array contents determines the final length of the array. string[] stringArray = new string[] { }; // During initialization, determine the length of the array. The length of the array can be assigned later. bool[] boolArray = new bool[4];
Store the element at index 0
boolArray[0] = true; boolArray[1] = false; boolArray[2] = false; boolArray[3] = true;
Use array elements
string str1 = stringArray[0]; string str2 = stringArray[1]; Console.WriteLine(str1 + str2);
//Modify array elements
floatArray[2] = 50.0f;
*** //Array out of bounds (C# exception)
When modifying an array element, when modifying an element that does not exist in the array:
//System.IndexOutOfRangeException: “Index exceeds array bounds.”
//floatArray[4] = 60.0F;
4. Multidimensional array:
Meaning: array nested array form, commonly used two-dimensional array
Two-digit array format: string [,] strArray =new string[number of arrays, number of array elements]
example:
int[] intArray = new int[2] {2,4}; int[,] intArray1 = new int[2, 3] { {1,2,3}, {5,6,7}};
Two-dimensional array access elements:
int val = a[2,3];
Modify elements of two-dimensional array:
a[2,3]=10;
Traverse two-dimensional array elements
for (int i = 0; i < studentArray.GetLength(0); i + + ) { for (int j = 0; j < studentArray.GetLength(1);j + + ) { Console.WriteLine(studentArray[i, j]); } }
//item represents the elements in the two-dimensional array foreach (var item in intArray1) { Console.WriteLine(item); }
Other multi-dimensional array formats: For example, three-dimensional array string [,,] strArray =new string[number of arrays, number of array elements]
int[,,] val = new int[3, 3, 3] { { { 10, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }, { { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18 } }, { { 19, 20, 21 }, {22, 23, 24 }, { 25, 26, 27 } } };
Three-dimensional array access elements:
int val = a[2,2,2];
Three-dimensional array modification elements:
a[2,2,2]=10;
Traverse three-dimensional array elements
int[,,] tempArray = new int[2, 2, 2] { { {1,2 },{3,4 } },{ {5,6 },{7,8 } } }; for (int i = 0; i < tempArray.GetLength(0); i + + ) { for (int j = 0; j < tempArray.GetLength(1); j + + ) { for (int k = 0; k < tempArray.GetLength(2); k + + ) { Console.WriteLine(tempArray[i,j,k]); } } }