Sabari M Sabari M
Updated date Jan 11, 2023
In this blog, we will see how to declare and initialize an empty array in C#

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

Sabari M
Sabari M
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

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