C#. Arrays. Part 4. Implicitly typed arrays. The var keyword




Arrays. Part 4. Implicitly typed arrays. The var keyword


Contents


Search other websites:

1. What variables are called implicitly typed?

Implicitly typed variables are variables for which there is no type definition when they are declared (for example, int, float). Instead of the type name, the keyword var is used.

When declaring an implicitly typed variable, the type of this variable is determined by the compiler based on the value by which it is initialized. If the initialization value is an integer, the variable gets the type int. If the initialization value is a floating-point value, the variable gets a double type, and so on.

So, an implicitly typed variable must be initialized when it is declared.

 

2. How is the implicitly typed one-dimensional array declared? The keyword var. Examples

In C#, you can declare implicitly typed arrays. Declaring implicitly typed arrays is like declaring implicitly typed single variables.

The general form of declaring an implicitly typed one-dimensional array:

var array_name = new[] { value1, value2, ..., valueN }

where

  • array_name – directly the name of the array;
  • ­ value1, value2, valueN – a set of values that the array items take. Based on the data type of these values, the type of array items is determined.

Example.

// example of declaring implicitly typed one-dimensional arrays
var A = new[] { 5, 10, 23, 16, 8 }; // type of items of the array - int, number of items - 5
var B = new[] { true, false, false }; // type of items bool, number items - 3
var C = new[] { 'A', ';', '\'', '\n', '5', 'z' }; // type of items char, number items - 6
var X = new[] { 2.86, 2.0, 3.5 }; // type of items double, number items - 3
int d;
bool b;
char c;
float f;

d = A[2]; // d = 23
b = B[1]; // b = false
c = C[4]; // c = '5'
f = (float)X[2]; // f = 3.5

 

3. Example of declaring and using an implicitly typed two-dimensional array
// implicitly typed two-dimensional array
// a two-dimensional array of size 4 * 3, the type of items - int
var MI = new[,] { { 3, 5, -4 },
                  { 2, -1, 0 },
                  { 4, 9, 3 },
                  { -11, -5, 91 } };

// a two-dimensional array of size 3*4, the type of items - char
var MC = new[,] { { '1', '2', '3', '4' },
                  { 'A', 'B', 'C', 'D' },
                  { 'e', 'f', 'g', 'h' } };

var MD = new[,] { { 0.2, 3.5 },
                  { 3.3, -3.88 } };
int i;
char c;
double d;

i = MI[2,1]; // i = 9
c = MC[0, 2]; // c = '3'
d = MD[1, 0]; // d = 3.3

 

4. Example of declaring and using an implicitly typed three-dimensional array

A three-dimensional array M of size 2×3×4 is declared.

// 3-dimensional typed array of size 2 * 3 * 4, the type of array items is double
var M = new[, ,] {
                     {
                         { 1.0, 2.3, 4.5, 9.2 },
                         { -2.339, 5.66, 101.01, 0.1 },
                         { 10.34, 11.26, -8.239, 0.2 }
                     },
                     {
                         { 0.39, -8.45, 9.34, 0.223 },
                         { 19.239, 10.65, -8.08, 44.33 },
                         { 1, 2, 3, 5 }
                     }
                 };
double d;

d = M[0, 2, 3]; // d = 0.2
d = M[0, 0, 0]; // d = 1.0

 

5. Example of declaring and using an implicitly typed stepped array

An implicitly typed stepped array is declared that has 3 rows. In row 1, the number of items is 6. In row 2, the number of items is 3. In row 3, the number of items is 5.

// implicitly typed stepped array
var A = new[] {
                  new[] { 1, 3, 4, 10, 23, 46 },
                  new[] { 2, -4, 8 },
                  new[] { 0, -1, 2, -3, 0 }
              };
int d;
d = A[0][2]; // d = 2
d = A[2][3]; // d = -3


 

6. An example of declaring and using an implicitly typed array of arrays
// implicitly typed array of arrays
// The array has 2 rows, in each row there are two arrays with dimensions 2 * 3 and 2 * 4
var B = new[] {
                  new[,] {
                             { 11, 12, 13 },
                             { 14, 15, 16 },
                         },
                  new[,] {
                             { 21, 22, 23, 24 },
                             { 25, 26, 27, 28 }
                         }
               };
int d;

d = B[0][1, 2]; // d = 16
d = B[1][0, 3]; // d = 24

 


Related topics