Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:25:28+00:00 2026-05-30T19:25:28+00:00

I was creating a C# method in visual studio that contained only a switch

  • 0

I was creating a C# method in visual studio that contained only a switch statement where each case returned a value. By personal habit, I put something similar to the following code:

private string SwitchMethod(int num)
    {
        switch (num)
        {
            case 0:
                return "result 1";
            case 1:
                return "result 2";
            case 2:
                return "result 3";
        }
        return "no result";
    }

My question is this: Which code will have better performance? The code above or below, or are the same? And why?

I would assume that because of compiler optimizations…they might just be the same…but I really don’t know.

private string SwitchMethod(int num)
    {
        switch (num)
        {
            case 0:
                return "result 1";
            case 1:
                return "result 2";
            case 2:
                return "result 3";
            default:
                return "no result";
        }
    }

REVISION:
It seems that I should be more specific: When compiled…will less efficient code be generated by one or the other?

I realize that the difference in performance may be insignificant…I’m just curious really.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:25:30+00:00Added an answer on May 30, 2026 at 7:25 pm
    public static string foo(int num)
            {
                switch (num)
                {
                    case 0:
                        return "result 1";
                    case 1:
                        return "result 2";
                    case 2:
                        return "result 3";
                }
                return "no result";
            }
    

    Becomes:

    .method public hidebysig static string  foo(int32 num) cil managed
    {
      // Code size       57 (0x39)
      .maxstack  1
      .locals init ([0] string CS$1$0000,
               [1] int32 CS$4$0001)
      IL_0000:  nop
      IL_0001:  ldarg.0
      IL_0002:  stloc.1
      IL_0003:  ldloc.1
      IL_0004:  switch     ( 
                            IL_0017,
                            IL_001f,
                            IL_0027)
      IL_0015:  br.s       IL_002f
      IL_0017:  ldstr      "result 1"
      IL_001c:  stloc.0
      IL_001d:  br.s       IL_0037
      IL_001f:  ldstr      "result 2"
      IL_0024:  stloc.0
      IL_0025:  br.s       IL_0037
      IL_0027:  ldstr      "result 3"
      IL_002c:  stloc.0
      IL_002d:  br.s       IL_0037
      IL_002f:  ldstr      "no result"
      IL_0034:  stloc.0
      IL_0035:  br.s       IL_0037
      IL_0037:  ldloc.0
      IL_0038:  ret
    } // end of method Program::foo
    

    Moving the return into a default case:

    .method public hidebysig static string  foo(int32 num) cil managed
    {
      // Code size       57 (0x39)
      .maxstack  1
      .locals init ([0] string CS$1$0000,
               [1] int32 CS$4$0001)
      IL_0000:  nop
      IL_0001:  ldarg.0
      IL_0002:  stloc.1
      IL_0003:  ldloc.1
      IL_0004:  switch     ( 
                            IL_0017,
                            IL_001f,
                            IL_0027)
      IL_0015:  br.s       IL_002f
      IL_0017:  ldstr      "result 1"
      IL_001c:  stloc.0
      IL_001d:  br.s       IL_0037
      IL_001f:  ldstr      "result 2"
      IL_0024:  stloc.0
      IL_0025:  br.s       IL_0037
      IL_0027:  ldstr      "result 3"
      IL_002c:  stloc.0
      IL_002d:  br.s       IL_0037
      IL_002f:  ldstr      "result 4"
      IL_0034:  stloc.0
      IL_0035:  br.s       IL_0037
      IL_0037:  ldloc.0
      IL_0038:  ret
    } // end of method Program::foo
    

    Exactly the same. No performance difference. I changed the “no result” to result 4 just to make sure the code was regenerated. Apparently the C# compiler optimizes it or it just ends up being equivalent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a method that collects accumulated totals throughout the month. The problem
Currently I have created a ABCFactory class that has a single method creating ABC
I'm creating a javascript method that populates lists depending on a radio button selected
Currently i am creating an extension method that accepts parameters. Using the below example,
I need help with creating a C# method that returns the index of the
Taken from Exercise 1: Creating Windows Phone Applications with Microsoft Visual Studio 2010 Express
I'm working on a solution in Visual Studio 2010 Ultimate that contains two C#
I am programming in Visual Studio .Net and using C#. I am creating my
I am creating an extension for Visual Studio 2008, and because I didn't want
I am having trouble creating a new edmx file using Visual Studio 2011 beta

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.