C bitarray LEARNOVITA

C# Array Tutorial | Create, Declare, Initialize

Last updated on 12th Aug 2022, Blog, Tutorials

About author

Priya Krishnan (C# Developer )

Priya Krishnan is a C# developer expert and subject specialist who has experience with Git, WPF, WinForms, C#,.Net, SQL, .NET Development, VB, .NET Framework,.NET Core, SVN, and Mercurial. His articles help the learners get insights into the domain.

(5.0) | 18759 Ratings 2197

c# new int

You are free to specify any number of elements for the array, but it will be of the type that has not been specified yet when you use the new int[] syntax. This syntax will initialise an array object with the name arr and a certain number of elements.A collection of elements of the same type that are ordered and stored in an array. Arrays have a fixed size. An array is a collection of variables of the same kind that are kept at contiguous memory locations. Although an array can be used to store a collection of data, it is typically more convenient to conceive of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1,…, and number99, you declare one array variable, such as numbers, and then use numbers[0], numbers[1], and…, numbers[99] to represent individual variables. This saves you the trouble of having to remember all of the individual variable names. An index is used to access a particular element included within an array.Each array is made up of a series of memory locations that are joined together. The address with the lowest number belongs to the first element, while the address with the highest number refers to the last element.

C# Array

Initialization of an Array:

When you declare an array, you have the option to initialise its elements at the same time. Because the length may be deduced from the number of elements in the initialization list, the length specifier is not required to be included. Take, for instance:

C#

  • the int[] array1 and set it to equal the new int[] minus 1, 3, 5, 7, and 9;

The line of code that follows displays a definition of a string array, where each element of the array is initialised with the name of a day:

C#

  • string[] weekDays = new string[] Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday;

When you initialise an array upon declaration, as demonstrated in the following code, you can avoid the new expression and the array type. This is illustrated by the following code. This type of array is referred to as an implicitly typed array:

C#

  • int[] array2 is equal to 1, 3, 5, 7, and 9; string is equal to [] weekDays2 equals “Sun,” “Mon,” “Tue,” “Wed,” “Thu,” and “Sat”; weekDays2 does not include Sunday.

It is possible to create an array variable without first constructing the array itself; nevertheless, the new operator is required whenever a new array is assigned to this variable. Take, for instance:

C#

  • array3 has been copied from int[]; array3 has been set to new int[]; array3 has been set to 1, 3, 5, 7, 9; array3 has been set to error;

Important Facts You Should Keep in Mind Regarding Arrays in C#:

  • In C#, each and every array is dynamically allocated on the fly.
  • Because arrays are treated as objects in C#, the term “member length” can be used to determine their size. This is distinct from C and C++, in which the sizeof operator is used to determine length.
  • A C# array variable can be declared in the same way as any other variable by appending [] following the data type.
  • The array’s variables are arranged in a hierarchy, and each variable has a numeric index that starts at 0.
  • An object of type System, an array in C# is an instance of that type.
  • The default values of elements of numeric arrays are set to zero, while the default values of reference type elements are set to null.
  • The components of a jagged array are reference types and are set to a null value by default.
  • Any type, including another array type, can be used as an element in an array.
  • Reference kinds are known as array types, and they are derived from the abstract base type known simply as Array. These types implement IEnumerable, and as part of that process, they apply the foreach iteration pattern to each and every array in C#.
string Array in c#

There are a few distinct approaches to both declaring and initialising arrays:

Example 1:

defining an array with a size of 5 / defining an array But not assigns values int[] intArray1 is going to be a new int[5];The above sentence declares and initialises an array of type int that has the capacity to store five instances of the type int. The size of the array is denoted by the presence of square brackets ([]).

Example 2:

constructing array with size 5, and simultaneously assigning / values to int[] intArray2 is defined as: intArray2 = new int[5] * 1, 2, 3, 4, 5;

The statement that follows is identical to the one that came before it, with the exception that it gives values to each index in.

Example 3:

establishing an array with five members, which / represents the size of an array int[] intArray3 = ‘1, 2, 3, 4, 5’;

An array of arrays is referred to as a jagged array in C#:

A jagged array is simply an array that contains other arrays. Instead of storing literal values, jagged arrays keep other arrays.The initialization of a jagged array consists of two sets of square brackets [][]. The size of an array is specified by the first bracket, and the dimensions of an array that is going to be stored are specified by the second bracket.

  • int[][] Array = new int[4][];
  • Array[14] = new int[3]{2, 4, 2};
  • Array[15] = new int[4]{5, 6, 7, 8 };
Array & Indices
  • using System;
  • ArrayApplication namespace {
  • ArrayAplication namespace { 
  • Main(string[] args) {
  • int [] n = new int[10]; /* n is an array of 10 integers */ int i,j; /*
  • Main(string[] args) {
  • /
  • * initialise entries of array n */ for I = 0; I < 10; i++) { n[i] = I + 100; }
  • {
  • /* display the value of each array element */ {
  • /* display the value of each array element */ 
  •  Console.WriteLine(“Element[{0}] = {1}”, j, n[j]); } Console.ReadKey(); 
  • }
  • output:
  • Element[0] = 201
  • Element[1] = 202
  • Element[2] = 203
  • Element[3] = 204
  • Element[4] = 205
  • Element[5] = 206
  • Element[6] = 207
    Senior NumberConcept Description
    1 Multi-dimensional arrays C# supports multidimensional arrays. The two-dimensional array is the most straightforward representation of the multidimensional array.
    2 Jagged arrays Multidimensional arrays, which are arrays that contain other arrays, are supported by C#.
    3 Arrays that are passed to functions You can provide the function with a pointer to an array by merely naming the array and omitting the index from the specification.
    4 Param arrays This is what you do when you want to send a function an undetermined number of parameters.
    5 The Array Class in Question It is the base class for all arrays, and it is defined in the System namespace. It comes with a variety of attributes and methods that may be used to manipulate arrays.
Array Representation

The Many Benefits of Using Arrays in C#:

  • a single name can be used to refer to a large number of data items that are all of the same kind.
  • Arrays are the building blocks for the implementation of various data structures, including linked lists, stacks, trees, and queues.
  • Matrices can be represented in C# using the two-dimensional arrays that are available.
  • As a result of arrays being strongly typed, the efficiency of the programme is enhanced because there is no need for boxing and unpacking.

The Drawbacks of Using Arrays in C#:

  • The size of the array cannot be changed. As a result, you are required to indicate the size of the array in advance.
  • Memory that is allocated in excess of what is necessary will be thrown away if you do not free it.
  • In no circumstance can a new element be added in the middle of an existing array.
  • It is also not feasible to delete or remove elements from the array while they are in the middle of being stored.

An overview of the array:

The following characteristics are possessed by an array:

  • One-dimensional, multidimensional, or jagged arrays are all possible forms that an array might have.
  • When an instance of an array is formed, the number of dimensions and the length of each dimension are both determined at that time. During the whole lifespan of the instance, these settings won’t be subject to any changes.
  • Numeric array elements have their default values set to zero, and reference array elements have their default values set to null.
  • A jagged array is an array that contains other arrays; hence, the elements of a jagged array are reference types and are set to null by default.
  • Arrays use a zero-based indexing system, which means that the elements of an array with n items are listed in order from 0 to n-1.
  • Any type, including another array type, can be used as an element in an array.
  • Reference types known as arrays are derived from the abstract base type known simply as Array.
  • IList and IEnumerable are interfaces that are implemented by every array.
  • Iterating across an array can be accomplished with the help of the foreach statement.
  • IListT> and IEnumerableT> are both interfaces that can be implemented by single-dimensional arrays.
    MethodDescription
    Copy(Array,Array,Int32) By providing a starting index, it allows one array to be used as a source for the copying of elements from another array.
    CopyTo(Array,Int32) is a function that transfers all of the elements of the current one-dimensional array to the one-dimensional array that is specified, beginning at the index that is specified for the destination array.

Are you looking training with Right Jobs?

Contact Us

Popular Courses