Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:29:44+00:00 2026-06-01T23:29:44+00:00

I have a class Foo in C# that has a string name and I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:29:46+00:00Added an answer on June 1, 2026 at 11:29 pm

    Your code doesn’t compile. Correcting it to:

    class Foo
    {
        static int count = 0;
        int fooNum;
        string name;
    
        public Foo()
        {
            ++count;
            fooNum = count;
            name = "Foo" + fooNum;
            Console.WriteLine(name);
        }
    }
    
    static void Main(string[] args)
    {
        for (int i = 0; i < 5; i++)
        {
            Foo test = new Foo();
        }
    }
    

    makes it compile and work like a charm. It prints

    Foo1
    Foo2
    Foo3
    Foo4
    Foo5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have one class Foo that has a bunch of logic in
I have a class Foo that has a method Bar makeBar(String id) . As
I have two classes, Foo and Bar. Each Foo has a name and a
I have a class (foo) that contains a vector. If i try iterating over
I have an entity class Foo foo that contains Collection<Bar> bars . I've tried
How to select next n hidden elements that have class foo using jQuery?
I have a Rack application that looks like this: class Foo def initialize(app) @app
I have several categories that I use in my Grails plugin. e.g., class Foo
Let's say I have a bunch of classes that look something like... class Foo{
I have two classes, Foo and Bar, that have constructors like this: class Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.