Cast a null into something?
Asked Answered
M

5

5

I had this interesting discussion today with a colleague. We were debating two pieces of code in C#.

Code Snippet 1:

if(!reader.IsDBNull(2))
{
  long? variable1 = reader.GetInt64(2)
}

Code Snippet 2:

long variable1 = reader.IsDBNull(2) ? (long?) null : reader.GetInt64(2)

Question is: is it a good practice to cast null into a nullable long? Or would you rather use the traditional if statement to avoid casting null to nullable long.

Mccrory answered 17/5, 2012 at 10:26 Comment(3)
Well the cast is for the Conditional Operator you use, it needs the return type of the branches to be similar and snippet2 is compiler error you need it to be long? variable1Whangee
As V4Vendetta said, in your second snippet your lh and rh types are different.Quartic
In snippet1, variable1 only exists inside the if's scope.Trumaine
K
15

The expressions (type?)null, default(type?) and new Nullable<type>() end up being compiled into the same opcodes:

        long? x = (long?)null;
        long? y = default(long?);
        long? z = new Nullable<long>();

is turned into:

    IL_0001: ldloca.s x
    IL_0003: initobj valuetype [mscorlib]System.Nullable`1<int64>
    IL_0009: ldloca.s y
    IL_000b: initobj valuetype [mscorlib]System.Nullable`1<int64>
    IL_0011: ldloca.s z
    IL_0013: initobj valuetype [mscorlib]System.Nullable`1<int64>

In other words, if you are working with nullable types, you are free to use whichever version you like best. Note however, that you should try to avoid arithmetics with nullable types. If you want to return a nullable value from a conditional expression, both possible results must be nullable if one of them can be null. Any other way could cause an exception in that case.

Keener answered 17/5, 2012 at 10:41 Comment(1)
For the sake of completeness: The expression null as type? is also compiled into that same IL code. In fact, null need not be casted explicitly if the surrounding expression provides a nullable type context, e.g. condition ? null : value as type?. The null in that expression is also compiled to the new Nullable<type>() IL code.Keener
C
3

Instead of

(long?) null

use

default(long?) 

I would refactor above code like

long? variable1 = reader.IsDBNull(2) ? default(long?) : reader.GetInt64(2)
Carmelocarmen answered 17/5, 2012 at 10:30 Comment(1)
Why are you using second brackets around long? ?Kortneykoruna
K
1

I prefer not to cast null value (it looks odd to me):

long? variable1 = reader.IsDBNull(2) ? null : (long?)reader.GetInt64(2);

Another options:

long? variable1 = reader.IsDBNull(2) ? default : reader.GetInt64(2); // requires C# 7.1
long? variable1 = reader.IsDBNull(2) ? default(long?) : reader.GetInt64(2);
long? variable1 = reader.IsDBNull(2) ? (long?)null : reader.GetInt64(2);
long? variable1 = reader.IsDBNull(2) ? new Nullable<long>() : reader.GetInt64(2);
long? variable1 = reader.IsDBNull(2) ? new long?() : reader.GetInt64(2);
long? variable1 = reader.IsDBNull(2) ? null : new long?(reader.GetInt64(2));

It's just the matter of taste. I think first option is more readable, than others.

UPDATE: Consider also writing some extension methods to make your code more clear:

public static class DataReaderExtensions
{
    public static long? GetNullableInt64(this IDataReader reader, int index)
    {
        if (reader.IsDBNull(index))
            return null;

        return reader.GetInt64(index);
    }
}

In this case you don't use ternary operator (no casting to nullable), and reading values from reader looks more pretty:

long? variable1 = reader.GetNullableInt64(2);
Kortneykoruna answered 17/5, 2012 at 10:29 Comment(6)
@lazyberezovsky Coz you cannot convert null to long?Whangee
@Whangee I fixed this typo before your first commentKortneykoruna
@lazyberezovsky you still have it up there ...? null : (long?)reader.GetInt64(2) :(Whangee
@Whangee up there long? variable1Kortneykoruna
@lazyberezovsky Do you have compiler near you , it should give no implicit conversion between <null> and long (you have to cast the null in your expression) Hope you get my point ! !Whangee
@Whangee can you show me the place where casting between null and long occurs?Kortneykoruna
P
0

Snippet 2 is worth in my case, as in case of null you're gonna get 0, which is a completely valid value for long

Pinwheel answered 17/5, 2012 at 10:30 Comment(0)
A
0

In C# 7.1 you can use the more concise default literal:

var variable1 = reader.IsDBNull(2) ? default : reader.GetInt64(2);
Arboretum answered 20/9, 2018 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.