Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I Create New Object inside Loop?

Please assist me.

Below find sample code

Which one is good?

Option 1 or 2 or 3 .

Thank you !!!

What I have tried:

C#
class A
{
    public void MethodA()
    {

        //--------------
    }
}


class B
{
    A a = new A();
    public void MethodB()
    {
        for (int i = 0; i < 500000; i++)
        {
            a.MethodA();
        }
    }
}


class B
{
    A a;
    public void MethodB()
    {
        for (int i = 0; i < 500000; i++)
        {
            a = new A();
            a.MethodA();
        }
    }
}


class B
{
    
    public void MethodB()
    {
        for (int i = 0; i < 500000; i++)
        {
            A a = new A();
            a.MethodA();
        }
    }
}
Posted
Updated 27-Jun-18 21:40pm
Comments
CHill60 28-Jun-18 3:37am    
I was always taught to keep the declaration as close as possible to where it is first required so for me it should be Hide   Copy Code
A a = new A();
inside the loop (the last option)... but be aware of variable scope
My Bad - see solution 1

The second and third options create 500,000 new objects of class A. Not a very good idea.
 
Share this answer
 
Comments
CHill60 28-Jun-18 3:39am    
Ah - good point. I missed that
Richard MacCutchan 28-Jun-18 4:01am    
But in a more complex (and useful) class, the second or third option could be the right way.
You can't compare them, they do different jobs, so there isn't a "best" or "good" one.
The first example
A a = new A();
public void MethodB()
{
    for (int i = 0; i < 500000; i++)
    {
        a.MethodA();
    }
}
Calls MethodA on teh same instance of the class every time.
The second example
A a;
public void MethodB()
{
    for (int i = 0; i < 500000; i++)
    {
        a = new A();
        a.MethodA();
    }
}
Creates a new instance for each time around the loop, and calls MethodA on that instance.

The difference is subtle, but if you think about cars then in the first instance you buy a car, and you drive it to work each day. In the second instance, you buy a new car each day and drive it to work, then discard it at the end of the day!

There is no "best" or "good" in that - for some approaches the first is right, for others it's the second.
 
Share this answer
 
v2
Comments
[no name] 28-Jun-18 4:15am    
Really Great, Thanks you !!!

So your are suggesting first one right ?
OriginalGriff 28-Jun-18 4:23am    
It depends on what you want to do!
If you need 500,000 separate instances, then the you need the second version.
For example, think of a library: each book would be a separate instance of the Book class, with it's own Title, Author, Shelf location, ISBN, etc. To construct the library, you would need 500,000 instances of the Book class!

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