How to Insert into List at Specified Index Position

vishalneeraj-24503
Posted by vishalneeraj-24503 under C# category on | Points: 40 | Views : 1227
We can Insert item in List collection at any specified Location or Position Index
With the help of Insert static method of List Collection,we can insert any item.

For Example:-

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Collections.Generic.List<string> names = new System.Collections.Generic.List<string>();

names.Add("Vishal "); //Output Would be:: Vishal
names.Add("Neeraj"); //Output Would be:: Vishal,Neeraj
names.Insert(1, "Kumar "); //Output Would be:: Vishal,Kumar,Neeraj
names.Insert(2, "Pune "); //Output Would be:: Vishal,Kumar,Pune,Neeraj

foreach (string name in names)
{
Response.Write(name);
}
}
}

Comments or Responses

Login to post response