
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How do you empty an array in C#?
To empty an array in C#, use the Array Clear() method: The Array.Clear method in C# clears i.e.zeros out all elements.
In the below example, we have first considered an array with three elements −
int[] arr = new int[] {88, 45, 76};
Now we have used the Array.Clear method to zero out all the arrays −
Array.Clear(arr, 0, arr.Length);
Let us see an example of Array.Clear method in c# −
Example
using System; class Program { static void Main() { int[] arr = new int[] {88, 45, 76}; Console.WriteLine("Array (Old):"); foreach (int val in arr) { Console.WriteLine(val); } Array.Clear(arr, 0, arr.Length); Console.WriteLine("Array (After using Clear):"); foreach (int val in arr) { Console.WriteLine(val); } } }
Output
Array (Old): 88 45 76 Array (After using Clear): 0 0 0
- Related Articles
- How do I empty an array in JavaScript?
- How do you create an empty list in Java?
- How do you sort an array in C# in ascending order?
- How do you sort an array in C# in descending order?
- How do you find the length of an array in C#?
- How do you make an array in Python?
- How to declare an empty string array in C#?
- How do you convert a list collection into an array in C#?
- How do you find the number of dimensions of an array in C#?
- How do you use ‘foreach’ loop for iterating over an array in C#?
- How do you convert an ArrayList to an array in Java?
- How do you limit an array sub-element in MongoDB?
- How to empty an array in Java
- How to empty an array in JavaScript?
- In Javascript how to empty an array

Advertisements