You're using the wrong data structure. Trying to use dynamic variable names is a bad idea, mostly not possible, and would make the code horrible in the attempts to use those variables. The vast majority of code written would be trying to hack together a working solution to the problem of naming a variable, entirely forgetting the actual domain logic you're trying to implement and the actual solution you're trying to build.
Data structures exist for such things. This sounds like a good use case for something like a Dictionary<string, Human>
instead. You can declare the collection in advance:
var humans = new Dictionary<string, Human>();
And then add key/value pairs where the name (from user input) is the (always unique) key:
humans.Add(newMemberName, new Human(newMemberName, 1));
Then you can always refer to that element from the collection by its name:
humans[newMemberName].SomeProperty = "some value";
Though there's a case to be made here that, unless there's a really good reason to use a Dictionary<>
(extremely fast lookups, guaranteed uniqueness of keys), you're still "duplicating data" because both the key and the "name" value in the object itself are the same value.
What happens if a Human
ever changes its name? The key is now wrong.
For small data sets and simple application logic, a List<Human>
will do just fine:
var humans = new List<Human>();
humans.Add(new Human(newMemberName, 1));
And you can query it by the name, or any other query you'd like to use:
humans.Single(h => h.Name == newMemberName).SomeProperty = "some value";