
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
Insert Item into a Chash List Using an Index
Firstly, set a list −
List<int> list = new List<int>(); list.Add(456); list.Add(321); list.Add(123); list.Add(877); list.Add(588); list.Add(459);
Now, to add an item at index 5, let say; for that, use the Insert() method −
list.Insert(5, 999);
Let us see the complete example −
Example
using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { List<int> list = new List<int>(); list.Add(456); list.Add(321); list.Add(123); list.Add(877); list.Add(588); list.Add(459); Console.Write("List: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine("
Count: {0}", list.Count); // inserting element at index 5 list.Insert(5, 999); Console.Write("
List after inserting a new element: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine("
Count: {0}", list.Count); } } }
Advertisements