Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get or set the element at the specified index in ArrayList in C#
To get or set the element at the specified index in ArrayList, the code is as follows −
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Laptop");
arrList.Add("Desktop");
arrList.Add("Notebook");
arrList.Add("Ultrabook");
arrList.Add("Tablet");
arrList.Add("Headphone");
arrList.Add("Speaker");
Console.WriteLine("Elements in ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 = " + arrList[5]);
}
}
Output
This will produce the following output −
Elements in ArrayList... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 5 = Headphone
Example
Let us see another example −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Laptop");
arrList.Add("Desktop");
arrList.Add("Notebook");
arrList.Add("Ultrabook");
arrList.Add("Tablet");
arrList.Add("Headphone");
arrList.Add("Speaker");
Console.WriteLine("Elements in ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 = " + arrList[5]);
arrList[5] = "SSD";
Console.WriteLine("Element at index 5 (Updated) = " + arrList[5]);
}
}
Output
This will produce the following output −
Elements in ArrayList... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 5 = Headphone Element at index 5 (Updated) = SSD
Advertisements