TechieClues TechieClues
Updated date Jan 11, 2023
In this blog, we will see how to declare and initialize an empty array in C#
  • 2.6k
  • 0
  • 0

In C#, there are multiple ways to declare and initialize an empty array. We will learn a few of them in this blog.

  • Using T[] array = {}
  • Using T[] array = new T[] {}
  • Using T[] array = new T[0]

Using T[] array = {}

using System;

namespace EmptyArray
{
    class Program
    {
        public static void Main()
        {
            string[] array = {  };

            Console.WriteLine(array.Length);
            Console.ReadKey();
        }
    }
}

Output:

0

Using T[] array = new T[] {}

using System;

namespace EmptyArray
{
    class Program
    {
        public static void Main()
        {
            string[] array = new string[] { };

            Console.WriteLine(array.Length);
            Console.ReadKey();
        }
    }
}

 Output:

0

Using T[] array = new T[0]

using System;

namespace EmptyArray
{
    class Program
    {
        public static void Main()
        {
            string[] array = new string[0];

            Console.WriteLine(array.Length);
            Console.ReadKey();
        }
    }
}

  Output:

0

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!