4

Consider the following statements:

int? v1 = null;

int? v2 = 5 * v1;

What is the value of v2? (null or empty string?)

How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling?

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
user160677
  • 4,233
  • 12
  • 42
  • 55
  • 4
    How can that be an empty string? – Kobi Aug 25 '09 at 12:22
  • 1
    @Kobi It certainly cannot. Maybe the asker was writing out the `v2` value somehow, and then it may become apparent that `v2.ToString() == ""`. The `Nullable<>` type overrides `ToString` and outputs `""` if `HasValue` is false. Note: This is relevant only if `ToString` is called directly, without boxing `v2` first (for example `IFormattable box = v2` will give a `null` reference, not a box; so `box.ToString("D4", CultureInfo.InvariantCulture)` will fail, trying to follow a `null` reference). – Jeppe Stig Nielsen Dec 18 '14 at 12:29
  • 1
    @Jeppe - Good point about `Nullable` and it's edge cases with `ToString` - I didn't know that, thanks! – Kobi Apr 11 '20 at 11:49

4 Answers4

19

It's null.

C# Language Specification 3.0 (Section §7.2.7: Lifted operators)

For the binary operators + - * / % & | ^ << >> :

a lifted form of an operator exists if the operand and result types are all non-nullable value types. The lifted form is constructed by adding a single ? modifier to each operand and result type. The lifted operator produces a null value if one or both operands are null (an exception being the & and | operators of the bool? type, as described in §7.10.3). Otherwise, the lifted operator unwraps the operands, applies the underlying operator, and wraps the result.


How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling?

It's not an invalid operation. It won't throw an exception so you don't need to handle exceptions in this case.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
11

If you want the operation to be prevented by the compiler, make the second variable non-nullable. Then you will be forced to write:

int v2 = 5 * v1.Value;

This will throw an exception at run time if v1 is null.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

Its value will be "null", in this context at least can be considered to be the same as a database null, i.e. rather than Nothing, or Zero, it means "Unknown". Five lots of Unknown is still Unknown, it'll also never be "empty string" as you're dealing with numbers, not strings.

I'm not sure what you mean by "How can I prevent the compiler to mark it as invalid operation?" as this code compiles and runs fine for me under Visual Studio 2008 =)

Rob
  • 45,296
  • 24
  • 122
  • 150
1

I'm not sure what you are trying to do, but if you want v2 to be null if v1 is null then you should test if v1 has a value prior to using it.

int? v2 = v1.HasValue ? 5 * v1.Value : null;

or

int? v2 = null;
if (v1.HasValue) { v2 = 5 * v1.Value; }
Donald Byrd
  • 7,668
  • 4
  • 33
  • 50
  • 1
    This is not true. You can simply multiply and achieve the same results. – Mehrdad Afshari Aug 25 '09 at 12:30
  • Yes, you are correct and I don't think I said otherwise. I was just indicating that you can and normally should test if the nullable value has a value before using said value. – Donald Byrd Aug 25 '09 at 14:53