Array (Array) in C #

In C #, arrays store a set of fixed-size elements in the same type. An array is used to store a data set, but it is often more useful to think of an array as a set of variables of the same type stored in adjacent memory locations.

Instead of declaring variables in a discrete way, like variables number0, number1 . and number99, you can declare an array of values ​​like numbers [0], numbers [1] and . numbers [99] to represent prices treatment separately. A specific member of the array can be accessed via index (index).

All arrays include adjacent memory locations. The lowest address corresponds to the first member and the highest address corresponding to the last member of the array.

Array (Array) in C # Picture 1

Declaring an array in C #

To declare an array in the C # language, you can use the syntax:

 ki ể u_d ữ _li ệ u [] t ê n_m ả ng ; 

In the above syntax:

  1. Data_type is used to specify the type of element in the array.
  2. [] rank determination or size of array.
  3. array_name identifies the array name.

For example:

 double [] balance ; 

Initialize arrays in C #

Declaring an array does not initialize arrays in memory. When the array variable is initialized, you can assign values ​​to that array.

Arrays are a reference type, so you need to use the new keyword in C # to create an Instance of that array. For example:

 double [] balance = new double [ 10 ]; 

Assign values ​​to an array in C #

You can assign values ​​to individual array elements using the array index, such as:

 double [] balance = new double [ 10 ]; balance [ 0 ] = 4500.0 ; 

You can assign values ​​to arrays at the time of array declaration, as follows:

 double [] balance = { 2340.0 , 4523.69 , 3421.0 }; 

You can also create and declare an array, as follows:

 int [] marks = new int [ 5 ] { 99 , 98 , 92 , 97 , 95 }; 

You can also skip array sizes, like:

 int [] marks = new int [] { 99 , 98 , 92 , 97 , 95 }; 

You can copy an array variable into another target array variable. In this situation, both the target variable and the source variable point to the same memory location:

 int [] marks = new int [] { 99 , 98 , 92 , 97 , 95 }; int [] score = marks ; 

When you create an array, the default C # compiler initializes each array element to a default value depending on the array type. For example, for an int array, all elements are initialized to 0.

Access array elements in C #

An element is accessed by the array index. This is done by placing the index of the element inside the square brackets after the array name. For example:

 double salary = balance [ 9 ]; 

The following example illustrates the concept of declaring, assigning and accessing arrays in C # mentioned above:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Mang trong C#" ); Console . WriteLine ( "-----------------------" ); int [] n = new int [ 10 ]; /* n la mot mang gom 10 so nguyen */ int i , j ; /* khoi tao cac phan tu cua mang n */ for ( i = 0 ; i < 10 ; i ++) { n [ i ] = i + 100 ; } /* hien thi gia tri cac phan tu cua mang n */ for ( j = 0 ; j < 10 ; j ++) { Console . WriteLine ( "Phan tu [{0}] = {1}" , j , n [ j ]); } Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

Array (Array) in C # Picture 2

Use foreach loop in C #

In the previous example, we used a for loop to access each element in the array. You can also use a foreach command to browse an array in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Mang trong C#" ); Console . WriteLine ( "-----------------------" ); int [] n = new int [ 10 ]; /* n la mot mang gom 10 so nguyen */ /* khoi tao cac phan tu trong mang n */ for ( int i = 0 ; i < 10 ; i ++) { n [ i ] = i + 100 ; } /* hien thi cac gia tri cua phan tu trong mang n */ foreach ( int j in n ) { int i = j - 100 ; Console . WriteLine ( "Phan tu [{0}] = {1}" , i , j ); i ++; } Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

Array (Array) in C # Picture 3

Array details in C #

Arrays are a very important part of the C # language. Here are the important array definitions that are presented more clearly for C # programmers:

Concept Description

Multidimensional array in C #

C # supports multidimensional arrays. The simplest pattern of multidimensional arrays is a two-dimensional array

Jagged array in C #

C # supports multidimensional arrays, which are arrays of arrays

Passing array to function in C #

You can pass a function pointer to an array by specifying the array name without the array index

Parameter array in C #

Used to pass an unknown number of parameters to a function

Array class in C #

Defined in the System namespace, it is the base class for all arrays, and provides properties and methods for working with arrays.

Follow tutorialspoint

Previous article: Converting data types in C #

Next lesson: String (String) in C #

4 ★ | 24 Vote

May be interested

  • Top 10 best array games for PCTop 10 best array games for PC
    top 10 best array games for pc you prefer to direct and command soldiers to occupy new bases and lands. simulate epic battles in history through strategy games, arrayed games. let's tipsmake.com find the top
  • Array of pointers in C ++Array of pointers in C ++
    before we understand the concept of pointer arrays, we consider the following example, which uses an array of 3 integer numbers.
  • How to Print an Array in JavaHow to Print an Array in Java
    if you are working on java and have an array with a large amount of data, you may want to print certain elements in order to view them conveniently. there are multiple ways you can print arrays in java and the examples given below will...
  • Indexer in C #Indexer in C #
    indexer in c # helps index objects, such as an array. when you define an indexer for a class, this class operates similarly to a virtual array. then you can access the instance of this class with the array access operator in c # ([]).
  • The Match function (the function searches for a specified value in an array or cell range) in ExcelThe Match function (the function searches for a specified value in an array or cell range) in Excel
    the match function helps you search for a specified value in an array, a range of cells, and then returns the position of the value in that array or range.
  • Use AI and the largest telescope to find aliens and interstellar lifeUse AI and the largest telescope to find aliens and interstellar life
    indian astronomers will play a key role in the 16-nation square kilometer array observatory (skao), whose dish antenna array began setting up this month and will scan the corners. distant universe in 2027.
  • MODE.MULT function - The function returns a vertical array of the most common values ​​in ExcelMODE.MULT function - The function returns a vertical array of the most common values ​​in Excel
    mode.mult function: the function returns a vertical array of the most frequently occurring values, or repeating values ​​in an array or data range. support functions from excel 2010 onwards. syntax: mode.mult ((number1, [number2], ...)
  • Advanced index operation in MongoDBAdvanced index operation in MongoDB
    create an index on the array ie create separate indexes for each of its fields. so in this situation, when we create an index on the array of tags, individual indexes will be created for their values: music, cricket and blogs.
  • Array data structureArray data structure
    array (array) is one of the oldest and most important data structures. arrays can store some fixed elements and these elements have the same type. most data structures use arrays to implement algorithms. here are the important concepts related to arrays.
  • Use array in ShellUse array in Shell
    a shell variable is able to hold a single value. this type of variables is called scalar variables.