Open In App

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: 
 

var iarray = new []{1, 2, 3};

Example 1: Below program illustrates how to use 1-Dimensional Implicitly 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);
    }
}

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

 


Article Tags :
C#