- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
- C# - Async and Await
- C# Modern Features
- C# - Tuples
- C# - Pattern Matching Enhancements
- C# - Nullable Reference Types
- C# - What's New in C# 11 / 12 / 13
- C# Practical & Advanced Usage
- C# - JSON & XML Handling
C# Array - Empty() Method
The C# Array Empty() method returns a singleton instance of an empty array without creating a new one each time.
Singleton is a creational design pattern that makes sure that only one object of its kind is available and provides a single point of access to it for any other code.
This method does not allocate memory for an empty array.
Syntax
Following is the syntax of the C# Array Empty() method −
public static T[] Empty<T> ();
Parameters
This method does not accepts any parameters −
Return value
This method returns an empty array of type T.
Example 1: Create an empty array of Integer
This is the basic example of the Empty() method, that shows how to create an empty array of integer −
using System;
class Program
{
static void Main()
{
// Create an empty array of integers
int[] emptyArray = Array.Empty<int>();
Console.WriteLine("Array length: " + emptyArray.Length);
}
}
Output
Following is the output −
Array length: 0
Example 2: Default value for optional parameter
In this example, Using the Empty() method for optional parameters in methods ensure you do not create a new array unnecessarily. −
using System;
class Program {
public void PrintNumbers(int[] numbers = null) {
if (numbers == null) {
numbers = Array.Empty<int>();
}
if (numbers.Length == 0) {
Console.WriteLine("No numbers to display.");
} else {
Console.WriteLine("Numbers: " + string.Join(", ", numbers));
}
}
static void Main() {
Program program = new Program();
program.PrintNumbers();
program.PrintNumbers(new int[] { 1, 2, 3 });
}
}
Output
Following is the output −
No numbers to display. Numbers: 1, 2, 3
Example 3: Array.Empty<T>() with Generic Collections
This example demonstrates how to use Empty() method in a complex scenario where it might be used with generics or a collection of a specific type −
using System;
class Program
{
static void ProcessArray<T>(T[] array)
{
Console.WriteLine("Processing array of type: " + typeof(T).Name);
Console.WriteLine("Array length: " + array.Length);
}
static void Main()
{
double[] emptyDoubleArray = new double[0];
ProcessArray(emptyDoubleArray);
}
}
Output
Following is the output −
Processing array of type: Double Array length: 0
Example 4: Create an Empty Array of Strings
Following is another example of the Empty() method. Here, we create an empty array of string −
using System;
class Program
{
static void Main()
{
string[] array = new string [] {"aman", "kumar", "gupta", "tutorialspoint"};
// create an empty string Array
string[] emptyArray = Array.Empty<string>();
Console.WriteLine("Array length: " + emptyArray.Length);
// display array element
for(int i =0; i<array.Length; i++){
Console.Write(array[i] + " ");
}
}
}
Output
Following is the output −
Array length: 0 aman kumar gupta tutorialspoint