when you declare an object, what are the numeric fields initialized to?

An array is a grouping of like-typed variables that are referred to by a common proper noun. And each data item is called an chemical element of the array. The information types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a face-to-face location. Length of the array specifies the number of elements present in the array. In C# the allocation of memory for the arrays is washed dynamically. And arrays are kinds of objects, therefore it is easy to detect their size using the predefined functions. The variables in the array are ordered and each has an index beginning from 0. Arrays in C# work differently than they do in C/C++.

Important Points to Remember About Arrays in C#

  • In C#, all arrays are dynamically allocated.
  • Since arrays are objects in C#, we can notice their length using member length. This is unlike from C/C++ where we find length using sizeof operator.
  • A C# array variable can as well be declared similar other variables with [] after the data type.
  • The variables in the array are ordered and each has an index kickoff from 0.
  • C# array is an object of base blazon System.Assortment.
  • Default values of numeric array and reference type elements are set to be respectively goose egg and null.
  • A jagged array elements are reference types and are initialized to goose egg.
  • Array elements can be of any blazon, including an array type.
  • Array types are reference types which are derived from the abstruse base type Array. These types implement IEnumerable and for it, they use foreach iteration on all arrays in C#.

The array has tin can contain primitive data types equally well every bit objects of a class depending on the definition of an array. Whenever utilise primitives data types, the bodily values have to be stored in face-to-face memory locations. In the example of objects of a grade, the actual objects are stored in the heap segment.

The following figure shows how assortment stores values sequentially :
C# Arrays

Explanation :
The index is starting from 0, which stores value. we tin can too store a fixed number of values in an array. Array index is to be increased past 1 in sequence whenever its not attain the array size.

Array Declaration

Syntax :

< Data Type > [ ] < Name_Array >        

Here,
< Data Type > : It define the chemical element type of the array.
[ ] : It define the size of the array.
< Name_Array > : It is the Name of array.

Example :

int[] ten;  // can shop int values string[] due south; // can store cord values double[] d; // tin can store double values Student[] stud1; // can store instances of Pupil course which is custom class        

Note : Just Declaration of an array doesn't classify retentivity to the array. For that array must be initialized.

Array Initialization

Every bit said earlier, an array is a reference type and then the new keyword used to create an instance of the array. Nosotros tin assign initialize individual array elements, with the help of the index.

Syntax :

type [ ] < Name_Array > = new < datatype > [size];        

Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and Name_Array is the proper name of an array variable. And new will allocate retentivity to an array according to its size.

Examples : To Prove Different ways for the Array Declaration and Initialization

Example ane :

// defining array with size 5.  // Only not assigns values int[] intArray1 = new int[5];        

The higher up statement declares & initializes int type assortment that tin store five int values. The assortment size is specified in square brackets([]).

Case two :

// defining array with size 5 and assigning // values at the same fourth dimension int[] intArray2 = new int[five]{1, ii, 3, 4, 5};        

The above statement is the aforementioned as, but it assigns values to each index in {}.

Example 3 :

// defining array with v elements which  // indicates the size of an array int[] intArray3 = {1, 2, iii, 4, 5};        

In the above argument, the value of the array is direct initialized without taking its size. Then, array size will automatically be the number of values which is directly taken.

Initialization of an Array after Declaration

Arrays tin can be initialized after the annunciation. It is non necessary to declare and initialize at the same time using the new keyword. However, Initializing an Array after the declaration, it must be initialized with the new keyword. It tin't be initialized by only assigning values.

Instance :

// Declaration of the array
cord[] str1, str2;

// Initialization of array
str1 = new string[5]{ "Chemical element i", "Element ii", "Element 3", "Element iv", "Chemical element 5" };

str2 = new string[five]{ "Element 1", "Chemical element ii", "Element three", "Element 4", "Element 5" };

Note : Initialization without giving size is not valid in C#. Information technology volition give a compile-time mistake.

Case : Wrong Declaration for initializing an array

// compile-time error: must requite size of an array
int[] intArray = new int[];

// error : wrong initialization of an assortment
cord[] str1;
str1 = {"Element 1", "Element 2", "Element iii", "Element 4" };

Accessing Assortment Elements

At the fourth dimension of initialization, we can assign the value. Merely, nosotros tin can also assign the value of the array using its index randomly after the declaration and initialization. Nosotros can access an array value through indexing, placed index of the chemical element within square brackets with the array proper name
Case :

//declares & initializes int type array int[] intArray = new int[5];  // assign the value 10 in array on its alphabetize 0 intArray[0] = x;   // assign the value 30 in array on its index 2 intArray[2] = 30;  // assign the value 20 in array on its alphabetize 1 intArray[1] = xx;  // assign the value fifty in array on its index iv intArray[iv] = 50;  // assign the value xl in array on its index 3 intArray[three] = twoscore;  // Accessing array elements using alphabetize intArray[0];  //returns ten intArray[2];  //returns 30        

Implementation : Accessing Array elements using different loops

using System;

namespace geeksforgeeks {

class GFG {

public static void Main()

{

int [] intArray;

intArray = new int [5];

intArray[0] = 10;

intArray[1] = 20;

intArray[2] = xxx;

intArray[iii] = forty;

intArray[4] = l;

Console.Write( "For loop :" );

for ( int i = 0; i < intArray.Length; i++)

Console.Write( " " + intArray[i]);

Console.WriteLine( "" );

Console.Write( "For-each loop :" );

foreach ( int i in intArray)

Console.Write( " " + i);

Panel.WriteLine( "" );

Console.Write( "while loop :" );

int j = 0;

while (j < intArray.Length) {

Console.Write( " " + intArray[j]);

j++;

}

Console.WriteLine( "" );

Panel.Write( "Do-while loop :" );

int yard = 0;

do

{

Console.Write( " " + intArray[k]);

k++;

} while (k < intArray.Length);

}

}

}

Output :

For loop : 10 20 30 40 50 For-each loop : 10 xx thirty 40 50 while loop : 10 twenty 30 40 fifty Practice-while loop : 10 twenty 30 40 50        

One Dimensional Array

In this array contains just one row for storing the values. All values of this array are stored contiguously starting from 0 to the array size. For instance, declaring a single-dimensional array of 5 integers :

int[] arrayint = new int[5];        

The in a higher place array contains the elements from arrayint[0] to arrayint[4]. Hither, the new operator has to create the array and also initialize its element by their default values. Above example, all elements are initialized by zero, Because it is the int type.

Instance :

using Arrangement;

namespace geeksforgeeks {

class GFG {

public static void Primary()

{

string [] weekDays;

weekDays = new string [] { "Dominicus" , "Mon" , "Tue" , "Midweek" ,

"Thu" , "Fri" , "Sat" };

foreach ( cord day in weekDays)

Console.Write(mean solar day + " " );

}

}

}

Output :

Lord's day Mon Tue Wed Thu Fri Sabbatum        

Multidimensional Arrays

The multi-dimensional array contains more than one row to store the values. It is also known every bit a Rectangular Assortment in C# because information technology's each row length is same. It tin can be a 2D-array or 3D-array or more. To storing and accessing the values of the array, i required the nested loop. The multi-dimensional array declaration, initialization and accessing is as follows :

// creates a ii-dimensional array of  // four rows and two columns. int[, ] intarray = new int[iv, 2];  //creates an array of three dimensions, 4, 2, and 3 int[,, ] intarray1 = new int[iv, 2, 3];        

Example :

using Arrangement;

namespace geeksforgeeks {

course GFG {

public static void Main()

{

int [, ] intarray = new int [, ] { { one, two },

{ iii, iv },

{ v, 6 },

{ 7, viii } };

int [, ] intarray_d = new int [4, 2] { { 1, 2 }, { 3, iv },

{ 5, half-dozen }, { vii, 8 } };

string [, ] str = new string [4, two] { { "i" , "two" },

{ "three" , "4" },

{ "v" , "six" },

{ "seven" , "eight" } };

int [,, ] intarray3D = new int [,, ] { { { 1, two, iii },

{ 4, 5, 6 } },

{ { vii, 8, nine },

{ 10, 11, 12 } } };

int [,, ] intarray3Dd = new int [two, two, three] { { { 1, 2, 3 },

{ 4, v, 6 } },

{ { 7, 8, 9 },

{ x, 11, 12 } } };

Console.WriteLine( "2DArray[0][0] : " + intarray[0, 0]);

Console.WriteLine( "2DArray[0][1] : " + intarray[0, 1]);

Console.WriteLine( "2DArray[1][1] : " + intarray[1, one]);

Console.WriteLine( "2DArray[2][0] " + intarray[2, 0]);

Console.WriteLine( "2DArray[1][ane] (other) : "

+ intarray_d[1, one]);

Console.WriteLine( "2DArray[ane][0] (other)"

+ intarray_d[1, 0]);

Console.WriteLine( "3DArray[one][0][ane] : "

+ intarray3D[1, 0, ane]);

Console.WriteLine( "3DArray[1][1][2] : "

+ intarray3D[ane, 1, 2]);

Console.WriteLine( "3DArray[0][1][ane] (other): "

+ intarray3Dd[0, one, 1]);

Console.WriteLine( "3DArray[1][0][2] (other): "

+ intarray3Dd[1, 0, 2]);

Console.WriteLine( "To String element" );

for ( int i = 0; i < 4; i++)

for ( int j = 0; j < ii; j++)

Console.Write(str[i, j] + " " );

}

}

}

Output :

2DArray[0][0] : 1 2DArray[0][1] : two 2DArray[1][1] : iv 2DArray[two][0] 5 2DArray[ane][1] (other) : iv 2DArray[1][0] (other)3 3DArray[1][0][one] : 8 3DArray[1][one][two] : 12 3DArray[0][1][one] (other): v 3DArray[1][0][2] (other): 9 To Cord element one two three four v 6 seven eight        

Jagged Arrays

An array whose elements are arrays is known as Jagged arrays information technology means "array of arrays". The jagged array elements may be of unlike dimensions and sizes. Below are the examples to show how to declare, initialize, and access the jagged arrays.

Instance :

using System;

namespace geeksforgeeks {

class GFG {

public static void Primary()

{

int [][] arr = new int [2][];

arr[0] = new int [five] { 1, 3, 5, 7, ix };

arr[1] = new int [iv] { ii, four, six, 8 };

int [][] arr1 = { new int [] { 1, 3, 5, 7, 9 },

new int [] { 2, 4, vi, 8 } };

for ( int i = 0; i < arr.Length; i++)

{

Organization.Console.Write( "Element [" + i + "] Assortment: " );

for ( int j = 0; j < arr[i].Length; j++)

Console.Write(arr[i][j] + " " );

Console.WriteLine();

}

Panel.WriteLine( "Another Array" );

for ( int i = 0; i < arr1.Length; i++)

{

Organisation.Panel.Write( "Element [" + i + "] Array: " );

for ( int j = 0; j < arr1[i].Length; j++)

Console.Write(arr1[i][j] + " " );

Console.WriteLine();

}

}

}

}

Output :

Element [0] Assortment: ane 3 5 7 ix  Element [1] Array: 2 4 6 8  Another Array Chemical element [0] Array: ane 3 5 7 ix  Element [i] Assortment: ii four half-dozen 8        

It'south possible to mix jagged and multidimensional arrays. The jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.
Example : To Declare and initialization of a single-dimensional jagged array which contains three ii-dimensional array elements of different sizes.

using Organization;

namespace geeksforgeeks {

form GFG {

public static void Principal()

{

int [][, ] arr = new int [3][, ] { new int [, ] {{1, 3}, {v, 7}},

new int [, ] {{0, ii}, {4, 6}, {viii, ten}},

new int [, ] {{eleven, 22}, {99, 88}, {0, 9}}};

for ( int i = 0; i < arr.Length; i++)

{

int x = 0;

for ( int j = 0; j < arr[i].GetLength(x); j++)

{

for ( int k = 0; grand < arr[j].Rank; k++)

Console.Write( " arr[" + i + "][" + j + ", " + grand + "]:"

+ arr[i][j, 1000] + " " );

Panel.WriteLine();

}

10++;

Panel.WriteLine();

}

}

}

}

Output :

          arr[0][0, 0]:1  arr[0][0, 1]:3   arr[0][i, 0]:5  arr[0][1, 1]:vii    arr[1][0, 0]:0  arr[i][0, i]:2   arr[1][1, 0]:4  arr[1][1, ane]:6   arr[one][2, 0]:8  arr[1][2, 1]:10    arr[2][0, 0]:xi  arr[2][0, one]:22   arr[2][1, 0]:99  arr[2][i, one]:88   arr[2][ii, 0]:0  arr[2][2, one]:9        

Points To Remember :

  • GetLength(int): returns the number of elements in the first dimension of the Array.
  • When using jagged arrays be condom every bit if the index does non exist then it volition throw exception which is IndexOutOfRange.

drummondaffir1949.blogspot.com

Source: https://www.geeksforgeeks.org/c-sharp-arrays/

0 Response to "when you declare an object, what are the numeric fields initialized to?"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel