Permalink
Browse files

[corlib] Optimize System.Nullable<T>.GetValueOrDefault().

Optimize System.Nullable<T>.GetValueOrDefault().

See discussion at:
	http://ericlippert.com/2012/12/20/nullable-micro-optimizations-part-one/

	If a variable of nullable value type is initialized with the
	default constructor then the [has_value] field will be its
	default value, false, and the value field will be default(T).

Since we know that Nullable<T>.value will be the default value when
the default constructor has executed, we can return it directly
instead of checking the has_value field.
  • Loading branch information...
1 parent 2d91700 commit 99a6d83c0b218137d20c6489c334e6ce78d6222e @jonpryor jonpryor committed Dec 20, 2012
Showing with 2 additions and 2 deletions.
  1. +1 −1 external/ikvm
  2. +1 −1 mcs/class/corlib/System/Nullable.cs
@@ -140,7 +140,7 @@ public override int GetHashCode ()
public T GetValueOrDefault ()
{
- return has_value ? value : default (T);
+ return value;
}
public T GetValueOrDefault (T defaultValue)

0 comments on commit 99a6d83

Please sign in to comment.