You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prefer Nullable<T>.GetValueOrDefault() over manual checking #33792
After checking a Nullable<T>.HasValue, it's common to see calls to Nullable<T>.Value; instead of calling Value, it's less work to call GetValueOrDefault(), as Value repeats the HasValue check. It's possible a future JIT could optimize away the duplicate check, but if nothing else using GetValueOrDefault() makes the job of the JIT easier.
This is a high-noise analyzer. GetValueOrDefault is preferable from a performance standpoint, but this does not matter for 99% of the code.
From a style perspective, Value is preferable because semantically is more correct than GetValueOrDefault. You never want a default. If anything, you want the explicit assertion in Value.
If many high-noise analyzers like this one are default-on then people will learn that the analyzer suite is not meaningful to their work.
What about correlation with IDE0030: UseCoalesceExpressionForNullable?
The rule suggest x ?? y instead of x != null ? x : y when x and y are nullable value types or nullable reference types.
For the new analyzer I'd prefer x.GetValueOrDefault(y) for value types - this clearly indicates that the type is value, which adds readability to the code. In the case IDE0030 should be changed to work only for reference types.
@xtqqczze the issue you linked doesn't seem like it's going to affect this analyzer. @iSazonov we can keep offering IDE0030 alongside this new fixer.
Suggested severity: Info
Milestone: Future
int?x=null;bool?b=null;// Before: The else has the exact same code as in the if body, but instead of x.Value it uses y, and is used only onceintp;if(x.HasValue){p=x.Value;}else{p=5;}// Afterp=x.GetValueOrDefault(5);// Before: There is no elseif(b.HasValue&&b.Value){DoSomething();}// Afterif(b.GetValueOrDefault()){DoSomething();}// Before: The else has the exact same code as in the if body, but instead of x.Value it uses y, and is used only onceinty=3;if(x.HasValue){Console.WriteLine(x.Value);}else{Console.WriteLine(y);}// AfterConsole.WriteLine(x.GetValueOrDefault(y));inte;// Before: A case where "IDE0030: Coalesce expression" will be offerede=x.HasValue?x.Value:y;// After: We continue offering the IDE0030 fixere=x??y;// But we also offer our new fixere=x.GetValueOrDefault(y);// Before: Difficult case (optional), where Nullable<T> is used in multiple places in the if and else, but the body is the sameintw=5;if(x.HasValue){Console.WriteLine(x.Value);Foo(x.Value);Bar();}else{Console.WriteLine(w);Foo(w);Bar();}// Afterintz=x.GetValueOrDefault(w);Console.WriteLine(z);Foo(z);Bar();// Code inside if/else is differentif(x.HasValue){Console.WriteLine(x.Value);}else{x=5;Foo(x.Value);}// Afterif(x.HasValue){Console.WriteLine(x.GetValueOrDefault());}else{x=5;Foo(x.Value);}
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
GSPP commentedon Jun 25, 2020
This is a high-noise analyzer.
GetValueOrDefaultis preferable from a performance standpoint, but this does not matter for 99% of the code.From a style perspective,
Valueis preferable because semantically is more correct thanGetValueOrDefault. You never want a default. If anything, you want the explicit assertion inValue.If many high-noise analyzers like this one are default-on then people will learn that the analyzer suite is not meaningful to their work.
stephentoub commentedon Jun 25, 2020
It wouldn't be on by default.
xtqqczze commentedon Nov 30, 2020
There is an existing code quality issue for
Nullable<T>.ValuefollowingNullable<T>.HasValue(#13699).iSazonov commentedon Dec 2, 2020
What about correlation with IDE0030: UseCoalesceExpressionForNullable?
The rule suggest
x ?? yinstead ofx != null ? x : ywhen x and y are nullable value types or nullable reference types.For the new analyzer I'd prefer
x.GetValueOrDefault(y)for value types - this clearly indicates that the type is value, which adds readability to the code. In the case IDE0030 should be changed to work only for reference types.carlossanlop commentedon Mar 2, 2021
@xtqqczze the issue you linked doesn't seem like it's going to affect this analyzer.
@iSazonov we can keep offering IDE0030 alongside this new fixer.
Suggested severity: Info
Milestone: Future
GetValueOrDefault for better performance
bartonjs commentedon Jul 27, 2021
Video
The feeling in the room during API review is we should just fix the JIT here. The resulting code this suggests looks rather unnatural.
xtqqczze commentedon Aug 21, 2021
This issue has been fixed in the JIT in .NET 6, see #13699 (comment).