5

If I have made a variable of a non-reference type, say int, nullable, i.e. int?, does this mean I need to use a constructor before assigning a value?

Normally to intialise a non-reference type variable I simply do

int foo = 5;

But if I have a nullable non-reference data type variable is initialisation neccessary, as below, or can I still use the simple initialisation above?

int? foo = new int();
foo = 5;
svick
  • 236,525
  • 50
  • 385
  • 514
Toby
  • 9,696
  • 16
  • 68
  • 132
  • Yes you can still use simple initialization. :) – Jenish Rabadiya Jun 16 '17 at 10:34
  • 5
    You could have just tried `int? foo = 5` and see how it goes, much faster than typing this question. – Evk Jun 16 '17 at 10:38
  • @Evk I could, but I wouldn't understand what was going on and if it might change in different circumstances - I'd rather ask and understand instead of just assuming :) – Toby Jun 16 '17 at 10:41
  • For others, I discovered this question's answer explains *why* @PatrickHoffman's answer is so. https://stackoverflow.com/a/3149180/1292918 – Toby Jun 16 '17 at 10:46
  • 1
    @Toby If you want to know *why* it works you could have simply looked at the documentation for the type, or, if doing some simple research didn't result in an answer (it would, if you had actually bothered to do any) you could have asked *why* the assignment works, rather than asking *if* it works, which, again, is something you could trivially have figured out in a matter of seconds. Wasting other people's time because you simply can't be bothered to spend a few seconds trying something or doing a simple web search is very inconsiderate. – Servy Jun 16 '17 at 13:30

2 Answers2

8

No. You don't need to create an instance before assignment. The int? is a struct which is created on assignment.

Your assignment foo = 5; is actually:

foo = new Nullable<int>(5);

This is all done by the compiler. No need to do this by yourself.

adjan
  • 13,371
  • 2
  • 31
  • 48
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 2
    @adjan, the question might be poor (in subjective terms) but it is not at all opinion based, some code either works or it doesn't. Furthermore, down-voting an answer because of your opinion of the question is not correct – Toby Jun 16 '17 at 10:44
  • 3
    @adjan, the question is perfectly allright and so the answer. I see your deleted answer which by itself is poor in nature and so probably got downvoted – Rahul Jun 16 '17 at 10:48
  • 2
    @Toby i edited your answer so i could remove the downvote. – adjan Jun 16 '17 at 10:52
2

int? is a syntax sugar for Nullable<int>; as for Nullable<T> if we have look at its implementation

https://referencesource.microsoft.com/#mscorlib/system/nullable.cs,ffebe438fd9cbf0e

we'll find an implicit operator declaration:

public struct Nullable<T> where T : struct
{
     ...

     [System.Runtime.Versioning.NonVersionable]
     public static implicit operator Nullable<T>(T value) {
         return new Nullable<T>(value);
     }

     ...
}

So for any struct T instead of explicit constructor call

T value = ...

T? test = new Nullable<T>(value);

we can use implicit operator

T? test = value; // implicit operation in action

In your particular case T is int and we have

int? foo = 5;
Toby
  • 9,696
  • 16
  • 68
  • 132
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    What you say sounds reasonable, except it's not actually true. When you write `int? foo = 5;`, the compiler does not use the implicit conversion operator. Instead, it directly calls the `Nullable` constructor. – svick Jun 16 '17 at 13:29
  • @svick: the implicit operator contians the constructor call only; C# compiler (optimizer?) just eliminates the intermediate call. – Dmitry Bychenko Jun 16 '17 at 13:41
  • 1
    I believe the C# compiler never performs that kind of inlining optimization, so that shouldn't be it. – svick Jun 16 '17 at 14:02