Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance degradation when we ALWAYS use nullable value types instead of value types?

Tags:

c#

Is there a performance degradation when we ALWAYS use nullable value types instead of value types?

like image 201
LaTeX Avatar asked Feb 05 '11 01:02

LaTeX


2 Answers

It's the price of copying the entire value type: pretty much nil for things like integers, GUID's, etc., but it's large if you're constantly copying 512-KB value types (which you shouldn't be having in the first place) inside a tight loop.

like image 126
user541686 Avatar answered Nov 14 '22 04:11

user541686


As Mitch Wheat pointed about above, no, you should not worry about this. I'm going to give you the short answer reason now, and later I'm going to help you discover more about what you're asking:

Write your code to be correct. Profile after writing so that you find the points that are causing you grief.

When you have code that constantly uses Nullable and you have performance reasons and you profile it and you can't find the problem yourself, then come ask us how to make it faster. But no, the overhead of using Nullable is for all intents and purposes not degrading.

Discover more about what you're asking:

  • Performance surprise with "as" and nullable types
  • Why shouldn't I always use nullable types in C#
  • C# Performance gain returning a Nullable Type from a SqlDataReader
  • Alternatives to nullable types in C#
  • Casting (int?)null vs. new int?() - Which is better?
  • Boxing / Unboxing Nullable Types - Why this implementation?

Now, having read all those pages, I hope you feel more enlightened.

like image 41
jcolebrand Avatar answered Nov 14 '22 05:11

jcolebrand