C# Renaming strings? || Giving a string the name of a string?

225 Views Asked by At

I'm learing C# right now using Visual Studio Community.

I'm trying to create a method that creates a variable amount of strings, each named differently. I want the name of the string to be "word" + wordcount. I predefined wordcount as 0 and it increases with each time I call this method. So the name would be: 1. word0 2. word1 …

After creating the string, I want to give it a word to hold and safe it inside an array. Though I think I would know how to do this step. (That's just some context for you.)

Is there a way to rename strings? I guess it would work by doing that, if there is one.

1

There are 1 best solutions below

0
On

As far as I know there is now way to that inc.

What you can use though, is an array. ` int n = 5; // you dynamic length, from user imput or computed int[] words = new int[n]; // create an array of that length

//set first (zeroth) element to 7
words[0] = 7;

// set the third (twoth) element to 5          
words[2] = 5;

//display all the elements
for (int i = 0; i < n; i++)
    Console.WriteLine(words[i]);

This seems to be pretty much what you asked for.