Click here to Skip to main content
15,312,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
List<string> data = {"a","b","c","d" ......}

this is my list . i need to add "cc" in every element of that list.

like this , data = {"acc","bcc","ccc","dcc" ......} so on ..

how can i make it dynamically ??

What I have tried:

List<string> data = {"a","b","c","d" ......}
int count = 50 ( i have 50 elements in list )
for (int i = 0; i < count ; i++)
{

}
Posted
Updated 27-Jun-21 19:28pm

You are going to have to learn to do some of this on your own ... this is trivial code you are asking for help with ...
C#
List<string> data = new List<string>{"a","b","c","d"};

for (int i = 0; i < data.Count; i++)
	{
    data[i] += "cc";
	}
   
Comments
George Swan 29-Jun-21 4:57am
   
If you want to have a collection that's dynamic, you can return an IEnumerable<string> so it only gets evaluated dynamically - that is, when you use it
List<string> data = new() { "a", "b", "c", "d" };
IEnumerable<string> ccStrings = data.Select(d => d + "cc");
you don't add new elements to the list - you extend the content of an element.
So it looks like :
C#
string s;

for (var i = 1; i <= 50; i++)
{
    s = data[i];
    data[i] = s + "cc";
}
   
v2
Comments
OriginalGriff 28-Jun-21 1:33am
   
Um ... Ralf ... I know it's early in the morning, but ... that won't compile! :laugh:
Ralf Meier 28-Jun-21 2:50am
   
Hi Griff,
the Brackets ? Right ?
But you are right - today it's really very early in the morning ... ;)
Richard Deeming 29-Jun-21 6:10am
   
Don't forget the range of the index. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900