Skip to main content

Posts

Showing posts from October, 2010

Nullable Value vs Casting.

Was talking to a team member half an hour ago, and he pointed out that he needs to change a line of code like this code (isLocked is a nullable bool)
bool isEnabled = (bool)isLocked

to

bool isEnabled = isLocked.Value

I asked him why you need to do it that way….and his answer was NO IDEA…
So thought of dissembling the IL to see what was really happening. This is the dissembled code I got from Reflector..
public struct Nullable where T: struct{ private bool hasValue; internal T value; public Nullable(T value) { this.value = value; this.hasValue = true; }
public bool HasValue { get { return this.hasValue; } } public T Value { get { if (!this.HasValue) } ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue); return this.value; }
public T GetValueOrDefault() { return this.value; }
}Digging it a little bit more, doing a casting on a nullable generates IL the same a…