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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:41:12+00:00 2026-05-23T18:41:12+00:00

My question is at the end of the following code in comments: using System;

  • 0

My question is at the end of the following code in comments:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
class MyObj
{
    int m_id;

    public MyObj(int id)
    {
        this.m_id = id;
    }

    public int GetId()
    {
        return this.m_id;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var m0 = new MyObj(0);
        var m1 = new MyObj(1);
        var m2 = new MyObj(2);
        var m3 = new MyObj(3);
        var m4 = new MyObj(4);


        List<MyObj> refList1 = new List<MyObj>()
            {
                m0, m1, m2, m3
            };

        List<MyObj> refList2 = new List<MyObj>()
            {
                m1, m2, m3, m4
            };


        //How to merge refList2 into refList1 without id repeating,
        //so refList1 must be [m0, m1, m2, m3, m4]
    }
}
}
  • 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-23T18:41:12+00:00Added an answer on May 23, 2026 at 6:41 pm

    You would do this using LINQ, with:

    var resultList = refList1.Union(refList2).ToList();
    

    However, this requires either that MyObj implements IEquatable<MyObj>, or the other overload of Union is used which takes an IEqualityComparer<T> parameter.

    The first solution would need this change:

    class MyObj : IEquatable<MyObj>
    {
        int m_id;
    
        public MyObj(int id)
        {
            this.m_id = id;
        }
    
        public int GetId()
        {
            return this.m_id;
        }
    
        public bool Equals(MyObj other)
        {
            if (ReferenceEquals(null, other)) {
                return false;
            }
            if (ReferenceEquals(this, other)) {
                return true;
            }
            return other.m_id == this.m_id;
        }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            if (obj.GetType() != typeof(MyObj)) {
                return false;
            }
            return Equals((MyObj)obj);
        }
    
        public override int GetHashCode()
        {
            return this.m_id;
        }
    }
    

    The second solution would go like:

    class MyComparer : IEqualityComparer<MyObj>
    {
        public bool Equals(MyObj x, MyObj y)
        {
            return x.GetId() == y.GetId();
        }
    
        public int GetHashCode(MyObj obj)
        {
            return obj.GetId();
        }
    }
    
    var resultList = refList1.Union(refList2, new MyComparer()).ToList();
    

    and would not require changes to class MyObj.

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

Sidebar

Related Questions

my question is, how fail-safe is the following code snippet which is part of
I have a quick question, suppose I have the following code and it's repeated
So, simple question really. :) I had this following code with ASPX View Engine
I have following model: class Customer < ActiveRecord::Base has_one :cart end class Cart <
Consider the following code: from numpy import log2 import matplotlib.pyplot as plt xdata =
Assume the following code: Stream file = files[0].InputStream; var FileLen = files[0].ContentLength; var b
The following code does not work due to line 4 (can't convert Foo into
Given the following code: // range heap example #include <iostream> #include <algorithm> #include <vector>
the following code: [1,3,5].to_csv => 1,3,5\n # this is good [[1,3,5], [2,4,6]].to_csv => 135,246\n
Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin

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.