I have a class Foo in C# that has a string name and I want each one to have a unique name. What I wanted to do is get the name from creating a static int variable and then assigning it to a local instance int variable to which I add to the end of the string. This does not work though, how would I be able to get my desired result.
class Foo
{
static int count = 0;
int fooNum;
string name;
public Foo
{
++count;
fooNum = count;
name = "Foo" + fooNum;
Console.WriteLine(name);
}
}
int main()
{
for(int i = 0; i < 5; i++)
{
Foo test = new Foo();
}
}
Actual Output:
Foo5
Foo5
Foo5
Foo5
Foo5
Desired Output:
Foo0
Foo1
Foo2
Foo3
Foo4
Any help would be greatly appreciated
Thanks
Your code doesn’t compile. Correcting it to:
makes it compile and work like a charm. It prints