C # array articles explain

xiaoxiao2021-03-06  63

Continue to go back, the array is usually used, I will introduce: arrays are a set of data with the same type. When the data in the array can be accessed, it can be specified by the subscript. The C # medium array element can be any data type, the array subscript starts from 0, that is, the first element corresponding to 0, and increment one by one. An array can also be multi-dimensionally. / / Contain one-dimensional integer array of 6 elements; int [] mf1 = new int [6]; // Note the range of initialization array, or specify the initial value; // contain one-dimensional integer array of six elements, initial value 1, 2, 3, 4, 5, 6 int [] mf2 = new int [6] {1, 2, 3, 4, 5, 6}; // One-dimensional string array, if the initial value setting is provided The NEW operator String [] MF3 = {"C", "C ", "c #"}; // one-dimensional object array Object [] mf4 = new object [5] {26, 27, 28 , 29, 30}; // Two-dimensional integer array, initial value MF5 [0,0] = 1, MF5 [0, 1] = 2, MF5 [1,0] = 3, MF5 [1, 1] = 4 INT [,] mf5 = new int [] {{1, 2}, {3, 4}}; // 6 * 6 2D integer array int [] mf6 = new mf [6,6]; Let's take a look at a one-dimensional string array traversal use system; public class mikecat {static void printArray (String [] arr) {// print array element, arr.length represents the number of for array elements (int i = 0; I

(For details, please refer to Mr. Tan Haoqiang's C language program) {for (int J = 0; j <2; j ) {console.writeline ("arr [{0}, {1}] = {2}", I, J, ARR [I, J]); // Print each 2D array element}}} public static void main ()} PUBLIC Static void main () {// Primitive // ​​Transfer array as a parameter (new int intelect) {{1, 2}, {3, 4}, {5, 6}, {7, 8}};}} Run Results: Arr [0,0] = 1 Arr [0, 1] = 2 Arr [1 0] = 3 Arr [1, 1] = 4 Arr [2,0] = 5 Arr [2, 1] = 6 Arr [3, 0] = 7 Arr [3, 1] = 8 class is an object-oriented program The basic construction block of the design is introduced later. Here we introduce two special classes, that is, the Object class and the String class 1.Object class Object class is the alias for the predefined class system.object, which is all other types of bases. All types in the class .C # are inherited directly or indirectly from the Object class. Therefore, the variable of an Object class can give any type of value. INT i = 30; Object Obj1; Obj1 = i; Object Obj2 = 'a'; 2 The. String class String class is specifically used to operate the string, he is the alias String str1 = "mikecat" for predefined class system.string; can be connected to the two strings with " ". String str2 = "Username:" "Mikecat"; if you access a single character, you should use the subscript. CHAR C = STR1 [0]; Compare the two strings are equal, the comparison operator "==" // is different from the Basic syntax BOOL B = (STR1 == STR2); C #, the most flexible use of C and C is also the most hard-to-master pointer. So how do you provide function pointers in C / C ? C # provides delegate (DELEGATE) The delegation is a reference type inherited from the System.Delegate class. It is equivalent to the function pointer. Unlike the function pointer, it is delegated to the C # is a type of security. The commission is particularly suitable for anonymous call. To use the entrustment needs to be three Steps, that is, statement, instantiation, call.

Using system; // Declare a delegate named MFDelegate, which has a string type parameter // C # compiler compiles a new class, inherits from System.Delegate, class // named mfdelegate public DeleGate Void mfdelegate; public class mikecat {// definition with mfdelegate has the same parameter type method Hello () public static void Hello ("Hello, {0}!", Name } // Define Method with MFDelegate has the same parameter type method Goodbye () public static void goodbye ("Goodbye, {0}!", Name); public static void main ()} public static void main ()} / Create a MFDelegate instance mf1 mfdelegate mf1 = new mfdelegate (Hello); // calls MF1 MF1 ("mikecat"); mfdelegate mf2 = new mfdelegate (Goodbye); MF2 ("mikecat"); // combine MF1 MF2 into one New commission MF3 MFDelegate MF3 = MF1 MF2; // Call MF3 MF3 ("Mike Old Cat"); // Remove MF1 MFDelegate MF4 = MF3-MF1; MF4 ("Mikecat") from the combined commissioned MF3;}} Procedure: Hello, Mikecat! // MF1; Goodbye, Mikecat! // MF2 Hello, Mike Old Cat! Goodbye, Mike Old Cat! // MF3 Goodbye, Mikecat! // MF4

转载请注明原文地址:https://www.9cbs.com/read-119175.html

New Post(0)