---
title: "Insert inside list in C#"  
description: "Insert inside list in C#"  
author: "Takeshi Okada"  
published: 2014-01-25  
updated: 2014-01-25  
canonical: https://www.mindstick.com/forum/1873/insert-inside-list-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Insert inside list in C#

I am initializing my list as below -

List<[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)> lFiles = new List<string>(12);

and [now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) I want to [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript)/[insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) my string at specific [index](https://www.mindstick.com/blog/198/index-in-sql-server).

like I am using below -

lFiles.Insert(6,"File.log.6");

it it throwing excepton as - "Index [must](https://www.mindstick.com/articles/12978/top-reasons-why-every-magento-ecommerce-store-must-opt-for-mobile-app) be within the bounds of the List."

While initializing I have declared [capacity](https://www.mindstick.com/interview/832/what-is-the-difference-between-the-size-and-capacity-of-a-vector) of List but still I am not able insert [strings](https://www.mindstick.com/forum/161912/explain-the-python-strings) at [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) indexes.

Anybody knows what I am [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on)??

## Replies

### Reply by Pravesh Singh

Hi Takeshi,

You are initializing the capacity of the list (basically setting the initial size of the internal array for performance purposes), but it does not actually add any elements to the list.

## The easiest way to check this is try this:

```
var list1 = new List<int>();var list2 = new List<int>(12);Console.WriteLine(list1.Count);  //output is 0Console.WriteLine(list2.Count);  //output is 0
```

This shows that you still don't have any elements in your list.

In order to initialize populate the array with default or blank elements, you need to actually put something into the list.

```
int count = 12;int value = 0List<T> list = new List<T>(count);list.AddRange(Enumerable.Repeat(value, count));
```


---

Original Source: https://www.mindstick.com/forum/1873/insert-inside-list-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
