0
public class Test 
{
    //Car c = null; // Is this better and is it good to set it to null.

    public void A()
    {
        Car c = new Car();
    }

    public void B()
    {
        Car c = new Car();
    }

    public void C()
    {
        Car c = new Car();
    }
}

1) Can I just declare Object 0 globally at the top and use it in each method instead of creating a new one each time (Is this what I am actually doing?)

2) Is it a good idea to initially set the object to null?

4
  • 3
    Please revisit your code and post something that compiles. Commented Oct 5, 2009 at 15:24
  • I quickly updated my code, don't know why this was downvoted. The main question is not about null, but mainly if re-declaration of objects will add to the size of my executable. Commented Oct 5, 2009 at 15:32
  • You should come back and take your time. You now have three local variables, none of which refer to the c class member object. Commented Oct 5, 2009 at 15:40
  • I commented out the c class member object to show I am not using it. If I had it declared, I would remove the local variables and just do something c = new Car(); Commented Oct 5, 2009 at 15:44

3 Answers 3

3

FxCop states that you should avoid unnecessary initialising code, because it generates unnecessary IL, which affects performance. (Although I think that the performance hit is very minimal). Sometimes, I also explicitly initialize variables, just to make my code more clear.

To answer your first question: it's all a matter of what you're trying / wanting to do ...

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this was the type of answer I was looking for. It seemed redundant to keep initializing the same object over and over again. Thanks again.
yeah, my C and C++ days make me initialize references to null, although I know it is unnecessary.
0

This is a duplicate question and has been asked many times on SO. The answer is, "it's a matter of style". Some prefer a style that indicates understanding of the fact that the compiler will initialize to null. Others prefer a style that makes it unnecessary to understand that the compiler will initialize to null.

2 Comments

That was part of my question.
Does re-declaration of objects increase the size of my executable or affect performance if I do it many times.
0

1) Can I just declare Object 0 globally at the top and use it in each method instead of creating a new one each time (Is this what I am actually doing?)

Apart from the invalid name for an object (compare 0 and O), yes, you can. Redeclaring creates another variable with local scope, it does not modify the private member of the class.

2) Is it a good idea to initially set the object to null?

It’s unnecessary since this is done automatically when an object of your class is created.

3 Comments

The invalid names come from me typing to fast.
Does redeclaration of objects add to the size of the executable?
Of course it does, a redaclaration creates a new (local) variable, wich of course needs to be saved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.