0

Assume that I have the following function

public static bool RequiresQuotes(this SqlDbType sqlType)
{
    switch (sqlType)
    {
        case SqlDbType.Char:
        case SqlDbType.NChar:
        case SqlDbType.NText:
        case SqlDbType.NVarChar:
        case SqlDbType.Text:
        case SqlDbType.VarChar:
        case SqlDbType.Xml:
        case SqlDbType.DateTime:
        case SqlDbType.SmallDateTime:
        case SqlDbType.Date:
        case SqlDbType.Time:
        case SqlDbType.DateTime2:
        case SqlDbType.DateTimeOffset:
            return true;
        default:
            return false;
    }
}

Does it make a performance difference to return true for each case rather than the above, or will it be optimized by the compiler?

For example if I call SqlDbType.Char.RequiresQuotes(); will it check all cases until it reaches SqlDbType.DateTimeOffset and then returns true or will only check the first case and then return true?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
user1492051
  • 886
  • 8
  • 22
  • Even though the duplicate's title may not exactly match your scenario, everything you need to know has been discussed before, the relevant term being "jump table". See [Is there any significant difference between using if/else and switch-case in C#?](http://stackoverflow.com/questions/395618/), [Is “else if” faster than “switch() case”? \[duplicate\]](http://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case), [Which is faster? (Race your horses)](http://ericlippert.com/2012/12/17/performance-rant/) and [Premature Optimization](http://c2.com/cgi/wiki?PrematureOptimization). – CodeCaster Dec 11 '15 at 11:59
  • @Jon I hope you weren't typing up an answer while I closed, if so, sorry. – CodeCaster Dec 11 '15 at 12:00
  • @CodeCaster: I was, but that's okay :) – Jon Skeet Dec 11 '15 at 12:00
  • @Jon feel free to reopen then. – CodeCaster Dec 11 '15 at 12:01
  • @CodeCaster: Nope, your dupe is fine. I don't always look for dupes as hard as I should :( – Jon Skeet Dec 11 '15 at 12:02
  • @Jon thanks. But then you'll never reach 1M rep... :P – CodeCaster Dec 11 '15 at 12:03

0 Answers0