
- 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 to delete/remove an element from a C# array?
To delete an elements from a C# array, we will shift the elements from the position the user want the element to delete.
Here, first we have 5 elements −
int[] arr = new int[5] {35, 50, 55, 77, 98};
Now let’s say we need to delete the element at 2nd position i.e. variable “pos = 2” is set, for that shift the elements after the specified position −
// Shifting elements for (i = pos-1; i < 4; i++) { arr[i] = arr[i + 1]; }
Now display the result as shown in the complete code below.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { static void Main() { int i = 0; int pos; int[] arr = new int[5] {35, 50, 55, 77, 98}; Console.WriteLine("Elements before deletion:"); for (i = 0; i < 5; i++) { Console.WriteLine("Element[" + (i) + "]: "+arr[i]); } // Let's say the position to delete the item is 2 i.e. arr[1] pos = 2; // Shifting elements for (i = pos-1; i < 4; i++) { arr[i] = arr[i + 1]; } Console.WriteLine("Elements after deletion: "); for (i = 0; i < 4; i++) { Console.WriteLine("Element[" + (i + 1) + "]: "+arr[i]); } Console.WriteLine(); } } }
Output
Elements before deletion: Element[0]: 35 Element[1]: 50 Element[2]: 55 Element[3]: 77 Element[4]: 98 Elements after deletion: Element[1]: 35 Element[2]: 55 Element[3]: 77 Element[4]: 98
- Related Articles
- How to delete element from an array in MongoDB?
- How to remove an element from an array in Java
- How to remove every Nth element from an array JavaScript?
- How to remove an element from Array List in C#?
- How to delete an element from an array in PHP and re-index the array?
- How to remove Specific Element from a Swift Array?
- How to remove an element from a doubly-nested array in a MongoDB document?
- How to delete elements from an array?
- How do I remove a particular element from an array in JavaScript
- How to remove a specific element from array in MongoDB?
- How to remove an element from a Java List?
- MongoDB query to match and remove element from an array?
- How to remove an element from a list in Python?
- How to remove a specific element from a JSON Array in Java?
- PHP program to delete an element from the array using the unset function

Advertisements