143

Explain why a nullable int can't be assigned the value of null e.g

int? accom = (accomStr == "noval" ? null  : Convert.ToInt32(accomStr));

What's wrong with that code?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mancmanomyst
  • 2,118
  • 6
  • 21
  • 23
  • You can assign null value to NullableInt you only need to use below syntax if val is your nullableint variable than val=new NullableInt(true); – CodeOptimizer May 02 '18 at 12:52

4 Answers4

281

The problem isn't that null cannot be assigned to an int?. The problem is that both values returned by the ternary operator must be the same type, or one must be implicitly convertible to the other. In this case, null cannot be implicitly converted to int nor vice-versus, so an explict cast is necessary. Try this instead:

int? accom = (accomStr == "noval" ? (int?)null : Convert.ToInt32(accomStr));
Harry Steinhilber
  • 5,219
  • 1
  • 25
  • 23
  • 5
    It's interesting - you don't actually need the cast for Convert.ToInt32... – Joe Ratzer Dec 01 '08 at 10:44
  • 3
    It's because System.Int32 != System.Nullable – Aaron Powell Dec 01 '08 at 10:59
  • 1
    Why does long? work without the cast? – David Radcliffe May 27 '10 at 13:49
  • 1
    As Joe R said, my cast on the Convert.ToInt32() was not necessary. There is an implicit cast from int to int?. The cast on the null is necessary regardless of whether you use int? or long?. The other options is to cast only the Convert.ToInt32() to an int? or long? (as suggested by Will Dean) as this will make the null convertible to the nullable type. The issue is that there isn't an implicit cast from => int or from int => . – Harry Steinhilber May 28 '10 at 17:47
  • 9
    i would prefer default(int?) over (int?)null; – Pauli Østerø Dec 11 '10 at 21:28
  • I just didn't realize, I had to convert null ... – BananaAcid May 18 '17 at 01:01
  • `'null's` also need to be casted.. : ) – Irf Feb 13 '18 at 04:06
  • I would prefer not to cast null because it is confusing to read. Instead of relying on implicit casting for the integer value, make it explicit. int? result = (condition) ? null: (int?)myInt; – Dan Bailiff Apr 09 '20 at 16:03
47

What Harry S says is exactly right, but

int? accom = (accomStr == "noval" ? null : (int?)Convert.ToInt32(accomStr));

would also do the trick. (We Resharper users can always spot each other in crowds...)

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • 3
    Now that is nice, that's just a subtle difference but sooo simple! Would +2 if I could (despite it being such a little thing) =) – Paul C Aug 21 '12 at 13:14
6

Another option is to use

int? accom = (accomStr == "noval" ? Convert.DBNull : Convert.ToInt32(accomStr); 

I like this one most.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 1
    Did you actually try this? It should give the error "Cannot implicitly convert type 'object' to 'int?'. An explicit conversion exists (are you missing a cast?)" – Ben Voigt Sep 07 '16 at 18:01
1

Similarly I did for long:

myLongVariable = (!string.IsNullOrEmpty(cbLong.SelectedItem.Value)) ? Convert.ToInt64(cbLong.SelectedItem.Value) : (long?)null;
Lobo
  • 4,001
  • 8
  • 37
  • 67
tenss
  • 11
  • 1