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

  • Home
  • SEARCH
  • 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 5976253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:12:07+00:00 2026-05-22T21:12:07+00:00

I am developing a game server in C# and a specific packet gets sent

  • 0

I am developing a game server in C# and a specific packet gets sent to my server 3 to 5 times per second per player. We will call that packet PacketA. I don’t do anything with it except make sure that I receive it.

Since this packet gets sent the most, I want to place it first in the switch block so I don’t have to make so many unnecessary comparisons. Will the compiler end up optimizing it away anyways (which will cause me to make all the comparisons down the switch block?)

switch (packetId)
{
    case PacketID.PacketA: // I do not want to do anything here.
        break;             // Just avoid all other packet id comparisons.

    case PacketID.PacketB:
        HandlePacketB(data);
        break;

    case PacketID.PacketC:
        HandlePacketC(data);
        break;

    // ...

    case PacketID.PacketZ:
       HandlePacketZ(data);
       break;
}

If the compiler does optimize this away, how can I change my code so I don’t have to check all the other packet ids?

  • 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-22T21:12:07+00:00Added an answer on May 22, 2026 at 9:12 pm

    The switch statement in C# is optimized in a way that allows for a very quick evaluation of where to go. It doesn’t read down through the list of options exactly. Instead it creates a jump list that it optimized for identifying which case to execute. Here is a link with more information about it:

    http://www.dotnetperls.com/switch-char

    Basically, I would suggest that you test the performance of this statement compared to another statement (such as doing an if statement for just that first case and then doing a switch statement if it isn’t PacketA). I would imagine that in the end the switch statement is the way to go.

    If you decide that you want to dig into your app by decompiling it, you can use the new (free) tool from Telerik:

    http://www.telerik.com/products/decompiling.aspx

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

Sidebar

Related Questions

We're developing a server system in Scala + Akka for a game that will
im developing a multi-player card game that needs the clients to communicate with each
I've been developing a Silverlight-c# client-server game that has now gotten to the point
I am developing a game for the web. The map of this game will
I'm developing an Asynchronous Game Server using .Net Socket Asynchronous Model( BeginAccept/EndAccept...etc.) The problem
I'm developing a social game in Flash with a team of developers. Our server-side
I'm developing a Flash game in ActionScript 2, and the issue if that this
In a multiplayer game I'm developing, we have a few values that are floating
I am developing game editor in C# that uses c++ lib files. I want
I'm developing a dedicated game server on a linux machine, in C/C++ (mixed). I

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.