2

I have a situation as below:

Some class

public class Numbers
{
}

Some other class

public class Calculations
{
  Numbers objNumber = null;

  public void abc()
  {
    objNumbers = new Numbers();
  }
}

What is wrong with the statement Numbers objNumber = null;? I know I could have simply ignored the null assignment; just curious why this is wrong.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
mukulsharma1146
  • 171
  • 1
  • 2
  • 12
  • What is your idea about _wrong_ in this case? Is your program error because of this null assignment? – Heinz Siahaan Jun 09 '16 at 04:04
  • There are no errors, I just need to the concept behind not initializing an Object type to Null. To put it in other words, What is the difference between Number obj = new Numbers(); and my case. – mukulsharma1146 Jun 09 '16 at 04:08

6 Answers6

8

It's not wrong, it's just unnecessary, all reference types are null by default, so there is no point on initializing the variable to null because the type of the variable is a reference type.

ryudice
  • 36,476
  • 32
  • 115
  • 163
2

Reference types in c#, when declared are initialized automatically with null value. So there is no difference in

Numbers objNumbers = null;

and

Numbers objNumbers;

Both will work the same. You can check this behavior by using a debugging the code.

vivek
  • 1,595
  • 2
  • 18
  • 35
  • Thanks Vivek, Will it impact the performance of the application in anyway ? – mukulsharma1146 Jun 09 '16 at 04:11
  • @mukulsharma1146, Not at all. One is implicitly done by compiler and in one case you are telling the compiler explicitly. So there will be no performace impact anyway. – vivek Jun 09 '16 at 04:13
1

I don't see any issue with what you are doing, just additional note types are created with default value. It won't make any difference assigning null or ignoring it.

I see just there is a typo, which you could easily identify and fix it.

There is an extra s in objNumbers, the field is defined as objNumber.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
1

The null will be the default value for the object(reference type). So It will be null even without the assignment by you. And there is nothing wrong with that statement.

In short

Numbers objNumbers = null; and Numbers objNumbers; are same.

Consider the following example;

Numbers objNumbers = new Numbers();
objNumbers = null;

Here assignment of null make Sense

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1

Strictly speaking it is not wrong, as the code compiles (provided you add an s to your field name).

However, there are multiple reasons this code might be unreliable from a perspective of design or correctness.

You code has a mutable field not initialized to any meaningful value: Consider the possibility that the method abc() may not get called prior to the value stored at objNumbers being accessed, which will result in a null reference exception for any likely use case.

Now consider following the null object pattern and initializing the field to an instance of this null version of the Numbers class.

That code would be much less likely to produce a null reference exception... and it would force you to think about your application's behavior in the case where accessing the field occurs prior to the execution of method abc.

For both these reasons, in my opinion, it is a favorable design discipline to avoid initializing your fields/properties to null.

Additional reading on the topic, such as this thread, shows dealing with nulls is often considered a problem in need of a solution. Initializing your code with instances and values instead of null is a start in the right direction.

Community
  • 1
  • 1
cocogorilla
  • 1,815
  • 14
  • 36
0

Don't think much. Even though you don't assign still all objects before allocating memory are null.