10

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

LaTeX
  • 1,411
  • 1
  • 17
  • 37
  • 1
    Overflow : not that you should worry about. – Mitch Wheat Feb 05 '11 at 01:29
  • to point out, I'm willing to bet that @MitchWheat knows a lot more about this than you so I'm gonna go with listen to him. @MitchWheat ~ You should've made that an answer :p ... – jcolebrand Feb 05 '11 at 01:31
  • 1
    Not as big of a performance hit as having an IStackOverflowUser implementation also inherit StackOverflow itself. – Novikov Feb 05 '11 at 01:32

2 Answers2

11

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:

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

Community
  • 1
  • 1
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • 1
    That's an `ul` unordered list - `ol` ordered lists are erhn... ordered (numbered) yah know? ;) – Yi Jiang Feb 05 '11 at 02:04
  • oh, facepalm, I started to type UL the first time :p – jcolebrand Feb 05 '11 at 02:22
  • 1
    What about memory? Since `obj.Value` can't be passed as a reference type (ref/in), it's necessary to assign the value to a new variable, doesn't this necess duplicating the value type as opposed to just assigning it to an ordinary type from the beginning? – Kresten Dec 03 '21 at 09:14
6

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.

user541686
  • 205,094
  • 128
  • 528
  • 886