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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:36:15+00:00 2026-05-23T09:36:15+00:00

Assume a generic List of type Packet , holding custom classes extending Packet ,

  • 0

Assume a generic List of type Packet, holding custom classes extending Packet, such as LoginPacket or ChatPacket.

Let’s say I put these in a list. When I take them out, the most ‘specific’ type each will be is Packet. I want to cast these into their more specific types though, back into their original LoginPacket or ChatPacket or back into whatever their original types were.

Question: How can I do this?

Reference: How to cast an object programmatically at runtime?

So…it seems like if these custom classes share a common interface, it would solve the casting problem? But if so, my classes can’t all share one interface. I need to build interfaces upon interfaces and classes upon classes. So…how would I do this? Sorry if this question isn’t so accurate. Not sure how to articulate my exact issue.

  • 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-23T09:36:15+00:00Added an answer on May 23, 2026 at 9:36 am

    You could have a whole long list of if/else or a switch

    if (item is ChatPacket)
    {
        // cast
    }
    else if (...)
    {
        // cast
    }
    else ...
    

    However, that can become unwieldly.

    You may wish to revisit your class design. By having a List<Packet>, you’re effectively stating you do not care about the differences between the derived children, at least in terms of their unique APIs.

    In other words, if your design is

    class Packet { public void Foo() { } }
    class ChatPacket : Packet { public void Bar() { } }
    

    You’re saying you only care about being able to access Foo().

    If you do care about differences, perhaps you can express those differences with polymorphism via abstract or virtual methods in the base and overriden behaviors in the children. Therefore you still have a collection of base classes, but you still get the custom behaviors as specified by each child.

    class Packet { public virtual void Foo() { } }
    class ChatPacket : Packet { public override void Foo() { } }
    

    Here, you call just call Foo(). No casting necessary.

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

Sidebar

Related Questions

Assume I have generic list L of some type in c#. Then, using linq,
I have a question about generic list. Assume that I have a database which
Does anyone know how to iterate over a generic list if the type of
Let's assume we have the following generic scenario: An RDBMS as a data source,
If I have a class using generic type such as public class Record<T> {
is there any possibility to have a generic List<System.Type> and to have a constraint
This is a question about generic c++ event driven applications design. Lets assume that
Assume you are doing something like the following List<string> myitems = new List<string> {
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
In Java, assume I have the following class Container that contains a list of

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.