开发者

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

Is there a performance degradation when we ALWAYS us开发者_如何学编程e nullable value types instead of value types?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜