csharp how to crete array

Code Example - csharp how to crete array

                
                        //you can apply arrays to any variable

//string array
string[] names = {"Guy", "Ryan", "Jim"};

//int array
int[] ages = {14, 16, 17, 19};

//double array
double[] timeRecord = {2.3, 5.6, 3.3};
                    
                
 

csharp create array

                        
                                // Define and Initialize
int[] arr = {1, 2, 3};

// Buuuut:
// Initial defining
int[] arr;

// This works
arr = new int[] {1, 2, 3};   

// This will cause an error
// arr = {1, 2, 3};
                            
                        
 

csharp define array

                        
                                float[] floats = new float[]{1.0,1.1,1.4,1.23,2212.233};
                            
                        
 

array declaration in csharp

                        
                                // Syntax: data_type[] variable_name = new data_type[size];

// Decalring array of integers of length 5
int[] intArr = new int[5];

// Declaring array of strings of length 5
string[] names = new string[5];

// We can also populate the array, if we know the elements in advance
int[] evenDigits = new int[] {0,2,4,6,8}; // notice that in this, we don't need explicit size declaration.
                            
                        
 

csharp inline array initialization

                        
                                var array = new string[2]; // creates array of length 2, default values
var array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2
var array = new[] { "A", "B" }; // created populated array of length 2