Home > Net >  C # define the difference between arrays and the C language definition
C # define the difference between arrays and the C language definition

Time:09-30

The points what there has been no specific area, bosses, please help, thank you

CodePudding user response:

C # from scratch to build an array index, namely the array index from scratch, in c # array works and works in most other popular languages are similar, but there are some differences should be noticed,
Declare an array, the square brackets ([]) must be followed in type, rather than behind the identifier, in c #, put the brackets after the identifier is not legal syntax,
Int [] table;//not int table [];
Another detail is that the size of the array is not part of its type in the C language it is part of an array type, this enables you to declare an array and to allocate the int object of arbitrary array, and regardless of the length of the array,
Int [] Numbers;//declare Numbers as an int array of any size
Numbers=new int [10].//Numbers is a 10 - element array
Numbers=new int [20].//now it 's a 20 - element array
The array
C # support one dimensional array, multidimensional arrays (rectangular array) and an array of arrays (staggered arrays), the following example shows how to declare a different type of the array:
A one-dimensional array:
Int [] Numbers;
Multi-dimensional array:
String [and] names;
An array of arrays (interlaced) :
Byte [] [] scores;
Declare an array (shown above) does not actually create them in c #, the array is an object (discussed later in this tutorial), must be instantiated, the following example shows how to create an array:
A one-dimensional array:
Int [] Numbers=new int [5].
Multi-dimensional array:
String [and] names=new string (5, 4];
An array of arrays (interlaced) :
Byte [] [] scores=new byte [5] [].
For (int x=0; X & lt; Scores. Length; X++)
{
Scores [x]=new byte [4];
}
Can also have a larger array, for example, can have three dimensional rectangular array:
Int [and] buttons=new int,5,3 [4];
Can even combine the rectangular array and jagged arrays, for example, the following code declares the type for two dimensional array of int three dimensional array of one dimensional array,
Int [], [and] [and] Numbers;
Initialize the array
C # by the initial value enclosed in curly braces ({}) to initialize array when statement provides a simple and straightforward method, the following example shows the initialization of an array of different types of various methods,
Note if wasn't initialize array in the statement, the members of the array is automatically initialized to the default initial value, the array type on the other hand, if the array declaration for a certain type of field, when to instantiate the type it will be set to the default value is null,
A one-dimensional array
Int [] Numbers=new int [5] {1, 2, 3, 4, 5};
String [] names=new string [3] {" Matt ", "Joanne", "Robert"};
Can omit the size of the array, as shown in the following:
Int [] Numbers=new int [] {1, 2, 3, 4, 5};
String [] names=new string [] {" Matt ", "Joanne", "Robert"};
If it provides initializers can omit new operator, as shown in the following:
Int [] Numbers={1, 2, 3, 4, 5};
String [] names={" Matt ", "Joanne", "Robert"};
Multi-dimensional array
Int [and] Numbers=new int [3, 2] {{1, 2}, {3, 4}, {5, 6}};
Siblings, string []=new string (2, 2] {{" Mike ", "Amy"}, {" Mary ", "Albert"}};
Can omit the size of the array, as shown in the following:
Int [and] Numbers=new int [and] {{1, 2}, {3, 4}, {5, 6}};
Siblings, string []=new string [and] {{" Mike ", "Amy"}, {" Mary ", "Albert"}};
If it provides initializers can omit new operator, as shown in the following:
Int [and] Numbers={{1, 2}, {3, 4}, {5, 6}};
Siblings, string []={{" Mike ", "Amy"}, {" Mary ", "Albert"}};
Crisscross array (array of arrays)
Can be like the example shown in initialization crisscross array:
Int [] [] Numbers=new int [2] [] {new int [] {4} 2, new int [],6,7,8,9 {5}};
Can omit the size of the first array, as shown in the following:
Numbers=new int int [] [] [] [] {new int [] {4} 2, new int [],6,7,8,9 {5}};
- or -
Int [] [] Numbers={new int [] {4} 2, new int [],6,7,8,9 {5}};
Please note that for staggered array elements not initialize the grammar,
Access to members of the array
Access to members of the array can be directly, similar to access members of the array in C/C + +, for example, the following code creates an array named Numbers, and then the fifth element in the array to 5:
Int [] Numbers={10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Numbers [4]=5;
The following code statement a multidimensional array, and is located in [1, 1] members assigned to 5:
Int [and] Numbers={{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Numbers (1, 1)=5;
The following statement a one-dimensional jagged arrays, it contains two elements, the first element is an array of two integers, the second element is the three arrays of integers:
Numbers=new int int [] [] [] [] {new int [] {1, 2}, new int [] {3, 4, 5}
};
The following statement to the first assignment of the first element of the array to 58, the second element in the second array to 667:
Numbers [0] [0]=58;
Numbers [1] [1]=667;
Arrays are objects
In c #, the Array is actually object, System. An Array is an Array type all abstract base type, you can use the System. An Array of attributes, and other members of the class, the usage of a sample is to use the "Length" (Length) properties for the Length of the Array, the following code Numbers to the Length of the Array (5) assigned to a variable called LengthOfNumbers:
Int [] Numbers={1, 2, 3, 4, 5};
Int LengthOfNumbers=Numbers. Length;
System. Array class provides many useful methods/other properties, such as for sorting, search and copy of the Array method,
An array using the foreach
C # also offers a foreach statement, the statement provides a simple, straightforward way to iterate through the array elements, for example, the following code creates an array named Numbers, using the foreach statement iterate through the array:
Int [] Numbers={4, 5, 6, 1, 2, 3, 2, 1, 0};
The foreach (int I in Numbers)
{
System. The Console. WriteLine (I);
}
Due to a multidimensional array, can use the same method to access elements, for example:
Int [and] Numbers=new int [3, 2] {{9, 99}, {3, 33}, {5, 55}};
The foreach (int I in Numbers)
{
The Console. Write (" {0} ", I);
}
The output of the sample are as follows:
9 99 3 33 5 55
However, with multidimensional arrays, using a nested for loop will allow you to better control the array elements,

CodePudding user response:

Need new C #, C language definition directly is ok,
Because c # array is an object that is only reference, cannot be used as the built-in types,
C # is the length of the array, and the C language is not the concept of length,
  •  Tags:  
  • C#
  • Related