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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:57:02+00:00 2026-05-25T02:57:02+00:00

This might not be possible without serializing the object to a byte array, but

  • 0

This might not be possible without serializing the object to a byte array, but I want to have a collection of objects where the only common properties will be an int and a bool:

public class Speakers : IStereoComponents
{
     public int Id { get; set; }
     public bool RemoveMe { get; set; }
     public string SomethingElse { get; set; }
}

public class Receiver : IStereoComponents
{
    public int Id { get; set; }
    public bool RemoveMe { get; set; }
    public IList<string> Buttons { get; set; }
}

public interface IStereoComponents
{
     int Id { get; set; }
     bool RemoveMe { get; set; }
}

What I want to do is be able to add these items to a list or dictionary and either remove or add them based on Id and the RemoveMe field. I want to be able to do something like:

foreach (var component in components) component.RemoveMe == true;

Is there an easy way to do this? The only way I can think of doing it is using reflection, serializing everything into a byte[], except for the Id and RemoveMe properties. This will be a chore, I was hoping there’s a fancy C# 4.0 of doing this? Or something completely obvious I’m missing?

  • 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-25T02:57:02+00:00Added an answer on May 25, 2026 at 2:57 am

    No need for reflection! Just make a List<IStereoComponents>:

    var components = new List<IStereoComponents>();
    components.Add(new Speakers { Id = 1, RemoveMe = false, SomethingElse = "100W" });
    components.Add(new Receiver { Id = 2, RemoveMe = false, Buttons = new List<string>() });
    

    Then, you can loop through them as you please:

    foreach (var component in components)
    {
        if (listOfIdsToRemove.Contains(component.Id))
            component.RemoveMe = true;
    }
    

    The beauty of interfaces is that you don’t really care what else the object can do: as long as it does what the interface requires, that’s enough.

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

Sidebar

Related Questions

Reading the documentation it seems this might not be possible, but it seems that
This might not have a major usecase in projects, but I was just trying
I'm not sure if this is possible in one mysql query so I might
This question might not seem programming related at first, but let me explain. I'm
This might be an easy one, but I'm not really experienced with sql. I
This might be a stupid question, but I was wondering whether or not you
I know this might seem a controversial question but it really is not meant
I feel like this might be really simple but I'm just not getting it
I know, this might be a very basic question but I am not 100%
Is it possible to create objects at designtime without having to have hard coded

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.