Since, engineers are mostly operating on algebraic operations the introduction to arrays and lists is needed. There is several types of the arrays in the C#. I will try show the most useful for me. In general the arrays and lists are used to store the data. I will try to show in easy way to how initialize the arrays.
1D array
For engineer can be understand as a vector. To defined the vector you need to declare the type name and size:
Type[ ] name = new Type[size]{ element_1, element_2, element_3, ... , element_size } ;
Let's say we need a 1D array of 4 double type numbers.
double[ ] array1 = new double[4]{ 1.02 , 2.32 , 0.000021 , 23123.3213} ;
The other way to declare the array is to put the elements in separate lines.
Type[] name = new Type[size] ;
name[0] = element_1 ;
name[1] = element_2 ;
name[2] = element_3 ;
...
name[size-1] = element_size ;
The example:
double[ ] array1 = new double[4];
array1[0] = 1.02;
array1[1] = 2.32;
array1[2] = 0.000021;
array1[3] = 23123.3213;
Example from VS:
Remember that you can not combine different types of elements inside one array.
2D array
Arrays can be n dimensional objects. To represent the matrix the 2 dimensional array can be build according to definition below.
Type[ , ] name = new Type[ size_1 , size_2 ]
{
{ element_1, element_2 } ,
{ element_3, element_4 }
};
or
Type[ , ] name = { { element_1, element_2 } , { element_3, element_4 } } ;
The other method of declaration is:
Type[ , ] name = new Type[ size_1 , size_2 ];
name[0,0] = element_1 ;
name[0,1] = element_2 ;
name[1,0] = element_3 ;
name[1,1] = element_4 ;
Example from VS:
1D array
For engineer can be understand as a vector. To defined the vector you need to declare the type name and size:
Type[ ] name = new Type[size]{ element_1, element_2, element_3, ... , element_size } ;
Let's say we need a 1D array of 4 double type numbers.
double[ ] array1 = new double[4]{ 1.02 , 2.32 , 0.000021 , 23123.3213} ;
The other way to declare the array is to put the elements in separate lines.
Type[] name = new Type[size] ;
name[0] = element_1 ;
name[1] = element_2 ;
name[2] = element_3 ;
...
name[size-1] = element_size ;
The example:
double[ ] array1 = new double[4];
array1[0] = 1.02;
array1[1] = 2.32;
array1[2] = 0.000021;
array1[3] = 23123.3213;
Example from VS:
Remember that you can not combine different types of elements inside one array.
2D array
Arrays can be n dimensional objects. To represent the matrix the 2 dimensional array can be build according to definition below.
Type[ , ] name = new Type[ size_1 , size_2 ]
{
{ element_1, element_2 } ,
{ element_3, element_4 }
};
or
Type[ , ] name = { { element_1, element_2 } , { element_3, element_4 } } ;
The other method of declaration is:
Type[ , ] name = new Type[ size_1 , size_2 ];
name[0,0] = element_1 ;
name[0,1] = element_2 ;
name[1,0] = element_3 ;
name[1,1] = element_4 ;
Example from VS:
Comments
Post a Comment