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 7083233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:09:11+00:00 2026-05-28T07:09:11+00:00

I am decoding comms messages from a binary stream. I create message objects of

  • 0

I am decoding comms messages from a binary stream. I create message objects of varying types depending on what messages have arrived. They all derive from a base CommsMessage type. All fine and dandy.

Elsewhere in my code I need to react to these messages, so I need to know what type of message it is.

Currently I am doing:

void ProcessIncomingMessage(CommsMessage msg)
{
    if (msg is MessageType1)
        return ProcessMessageType1(msg as MessageType1);

    if (msg is MessageType2)
        return ProcessMessageType2(msg as MessageType2);

    //etc
}

I am wondering what the performance cost of comparing these types are, and whether I should include a MessageType property in the base class instead. Then I could do:

void ProcessIncomingMessage(CommsMessage msg)
{
    switch (msg.MessageType)
    {
         case MessageType.Type1:  return ProcessMessageType1(msg as MessageType1);
         case MessageType.Type2:  return ProcessMessageType2(msg as MessageType2);

         //etc
    }
}

Yes, this is premature optimisation, and I’m probably worrying over insignificant details, but I’m the kind of coder who likes to know what’s going on under the covers and so was wondering the performance differences between the two. I guess I have a prejudice against type comparisons from my C++ background where RTTI introduced an overhead, and just wondered if .Net had any similarities.

  • 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-28T07:09:11+00:00Added an answer on May 28, 2026 at 7:09 am

    Have you considered eliminating the type casts?

    I’m guessing you’ve considered that putting the virtual method on the Message type itself would break a layering abstraction (e.g. you might want a clean separation of the processing of the message from the message itself). Maybe consider the visitor pattern. This’ll allow you to separate out the Message class from the processing of the Message itself.

    If you have something of this structure.

    abstract class CommsMessage {}
    class Message1 : CommsMessage {}
    class Message2 : CommsMessage {}
    

    You could refactor to

    abstract class CommsMessage 
    { 
        public abstract void Visit(CommsMessageVisitor v);
    }
    
    class Message1 : CommsMessage 
    {
        public void Visit(CommsMessageVisitor v) { v.Accept(this); }
    }
    
    class Message2 : CommsMessage 
    {
        public void Visit(CommsMessageVisitor v) { v.Accept(this); }
    }
    
    interface CommsMessageVisitor 
    {
       void Accept(Message1 msg1);
       void Accept(Message1 msg2);
    }
    

    At this point, you’ve eliminated the type-casts. You can now rewrite your code as

    void ProcessIncomingMessage(CommsMessage msg) 
    {
      new MyVisitor().Visit(msg);
    }
    
    class MyVisitor : CommsMessageVisitor
    {
        void Accept(Message1 msg1) { ProcessMessageType1(msg1); }
        void Accept(Message1 msg2) { ProcessMessageType2(msg2); }
    }
    

    Of course there might be reasons you can’t do this, but it’s always nicer to avoid type casts if you can!

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

Sidebar

Related Questions

I am decoding video frames from a stream and sws_scale gives me a buffer
I have read that decoding the potentially long string which can be returned from
I have some problem for decoding image data from base 64 encoded string. I
I've a little question about decoding special characters from a JSon result (in my
I have a DLL which provided a decoding function, as follows: function MyDecode (Source:
In my binary to text decoding application (.NET 2.0) I found that the line:
Aside from using a hardware video encoding/decoding device, is there an easy was to
I have a project that uses Boost.Asio and the Media-Decoding-Samples that come with the
I have a json structure that I'm decoding that looks like this: person =>
I'm decoding some JSON (from the Youtube data API) with json_decode and it gives

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.