Javatpoint Logo
Javatpoint Logo

Gets or Sets the element at the specified index in the List C#

The List<T> class of C# programming is one of several collection types commonly used, which offers dynamic arrays to simplify data management and manipulation. When working with lists, an often-performed task is to access and update the elements at given indices.

In this blog, we will discuss the ideas of getting and setting elements at given indices in a C# List.

Getting an Element at a Specified Index:

A C# List's indexer can be used to get an element at a particular index in the list. With the indexer, we can access elements in a list using square brackets and an index of an element required. Here's the syntax for getting an element at a specified index:

// Syntax:

In this syntax, myList represents an instance of the List <T> class while the index designates the position for the element to be retrieved.

Example:

Output:

Element at index 2: 30

In this example, there is a List that is created named "numbers" with initial values. Using the list's indexer, the element at index 2 (30 in this case) is retrieved and stored in the variable "elementAtIndex2." The program then prints the result, displaying "Element at index 2: 30. This example shows how to get a specific element from the List, thereby proving that it is very easy and fast to use the List<T> class in C# for array manipulation.

Setting an Element at a Specified Index:

Similarly, to set an element at a given index in a C#, List we use the same indexer followed by an assignment statement. The syntax for setting an element at a specified index is as follows:

// Syntax:

In this case, the myList is an instance of the List<T> class where we want to assign a new value at the location index equals the position where we are putting a new value, and it would be represented by a variable called #newVaule#.

Example:

Output:

Original list:
Apple
Banana
Cherry
Date
Fig
 
Modified list:
Apple
Banana
Grapes
Date
Fig

Explanation:

This example uses a List of strings named fruits with initial values. The original list is presented in the form of a foreach loop. Next, a new value ("Grapes") is assigned to index 2 by using the list's indexer. The modified list is then shown. The program features basic list manipulation, displaying how to change items dynamically. The output shows original and altered lists, focusing on the manipulation made to the second element from "Cherry" to "Grapes." This lean program can serve as an example of how to modify elements inside a List in C#.

Conclusion:

In conclusion, the C# programs presented above reveal that the List<T> class offers the flexibility of a dynamic array while providing easy-to-use functions. Whether it is about elements at specific index positions. Lists provide a simple and fast opportunity. The indexer makes the code much readable with it easy to access items. In the first case, the given program demonstrates how one gets an element at a specific index within the List, and that List is vital, as this is direct access.

In the second case of the program, Lists show its capability to set new values dynamically at a specific index position that is proof of List <T> class implementation for dynamic data updates. Both instances highlight the practicality of using Lists in data collection management, which is very fundamental for C# operational processes. Lists are essential elements of the C# program, which enable programmers to manage data efficiently and make it possible to develop high-quality applications. Collectively, these examples provide invaluable highlights to the effortless inclusion of Lists into C# programming language with respect to successful arrays implementation.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA