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
Comments (0)