Tutorialspoint.dev

C# | Implicitly Typed Arrays

Implicitly typed arrays are those arrays in which the type of the array is deduced from the element specified in the array initializer. The implicitly typed arrays are similar to implicitly typed variable. In general, implicitly typed arrays are used in the query expression.

Important points about implicitly typed arrays:

  • In C#, the implicitly typed arrays do not contain any specific data type.
  • In implicitly typed array, when the user initializes the arrays with any data type then compiler automatically convert these arrays into that data type.
  • Implicitly typed arrays generally declared using var keyword, here var does not follow []. For Example:
    var iarray = new []{1, 2, 3};
  • All types of array-like 1-D, Multidimensional, and Jagged Arrays etc. can be created as an implicitly typed array.
  • In C#, it is necessary to initialize implicitly typed array and have the same data type.

Example 1: Below program illustrate how to use 1-Dimesional Implicitlt typed array.

// C# program to illustrate
// 1-D implicitly typed array
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing 1-D
        // implicitly typed array
        var author_names = new[] {"Shilpa", "Soniya",
                                  "Shivi", "Ritika"};
  
        Console.WriteLine("List of Authors is: ");
  
        // Display the data of the given array
        foreach(string data in author_names)
        {
            Console.WriteLine(data);
        }
    }
}

Output:

List of Authors is: 
Shilpa
Soniya
Shivi
Ritika

Example 2: Below program illustrate the use of Multidimensional implicitly typed arrays.



// C# program to illustrate
// 2-D implicitly typed array
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing 
        // 2-D implicitly typed array
        var language = new[, ] { {"C", "Java"}, 
                            {"Python", "C#"} };
  
        Console.WriteLine("Programming Languages: ");
   
        // taking a string
        string a;
  
        // Display the value at index [1, 0]
        a = language[1, 0];
        Console.WriteLine(a);
  
        // Display the value at index [0, 2]
        a = language[0, 1];
        Console.WriteLine(a);
    }
}

/div>
Output:

Programming Languages: 
Python
Java

Example 3: Below code demonstrate the use of Implicitly typed jagged arrays.

// C# program to illustrate
// implicitly typed jagged array
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing 
        // implicitly typed jagged array
        var jarray = new[] {
            new[] { 785, 721, 344, 123 },
            new[] { 234, 600 },
            new[] { 34, 545, 808 },
            new[] { 200, 220 }
        };
  
        Console.WriteLine("Data of jagged array is :");
  
        // Display the data of array
        for (int a = 0; a < jarray.Length; a++) {
            for (int b = 0; b < jarray[a].Length; b++)
                Console.Write(jarray[a][b] + " ");
            Console.WriteLine();
        }
    }
}

Output:

Data of jagged array is :
785 721 344 123 
234 600 
34 545 808 
200 220


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter