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.
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:
- Data_type is used to specify the type of element in the array.
- [] rank determination or size of array.
- 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:
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 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:
Multidimensional array in C #
C # supports multidimensional arrays. The simplest pattern of multidimensional arrays is a two-dimensional arrayJagged array in C #
C # supports multidimensional arrays, which are arrays of arraysPassing array to function in C #
You can pass a function pointer to an array by specifying the array name without the array indexParameter array in C #
Used to pass an unknown number of parameters to a functionArray 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 #
You should read it
- Introduction to 2D Array - 2-dimensional array in JavaScript
- Array in Language C
- Array in Python
- How to Print an Array in Java
- Array formulas in Excel - Tutorials and examples
- Learn about Collection of Record in JavaScript
- Array (Array) in PHP
- Instructions and examples of array formulas Array Formulas in Excel
May be interested
- Top 10 best array games for PCtop 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 ++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 Javaif 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 # 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 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 lifeindian 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 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 MongoDBcreate 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 (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 Shella shell variable is able to hold a single value. this type of variables is called scalar variables.