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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:05:38+00:00 2026-05-16T00:05:38+00:00

I have 2 classes, X and Y. Both classes have same similar property like

  • 0

I have 2 classes, X and Y. Both classes have same similar property like below.

class X
{
    public string T1 { get; set; }
    public string T2 { get; set; }
    public string T3 { get; set; }
}

class Y
{
    public string T1 { get; set; }
    public string T2 { get; set; }
    public string T3 { get; set; }

    public string O1 { get; set; }
}

I’ve couple hundreds classes similar to X and Y; similar structure, and I decide to create generic class for this problem.

I have list of X and Y and I want to compare them by T1; only 1 property, to find out which element exist on both list, which element exist only on X and only on Y.

How can I do this?

  • 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-16T00:05:39+00:00Added an answer on May 16, 2026 at 12:05 am

    The best thing to do is to first create an interface that contains T1 only. Then you inherit each class like X and Y from this interface. Now you can easily create your generic classes or any helper classes based on this interface.

    Alternatively, you may use reflection, or if you use C# 4.0, you can use dynamic. Classic reflection is way to slow for (large) lists, so unless you cache your method calls, you shouldn’t take that approach. C# 4.0 however, provided method caching through the DLR, which is sufficiently fast in most cases.

    Alternatively (2): when you want to do this “right” and you want to compare the lists using standard mechanisms like LINQ, you should implement IComparable. You can combinee that with generics to create type-safety.

    // the interface, inherit from IComparable
    public interface IX : IComparable<IX>
    {
        string T1 { get; set; }
    }
    
    // create one base class
    class XBase : IX
    {
        public string T1 { get; set; }
        public int CompareTo(IX obj)
        {
            return this.T1.equals(obj.T1);
        }
    }
    
    // inherit all others from base class
    class X : XBase
    {
        public string T2 { get; set; }
        public string T3 { get; set; }
    }
    
    class Y : XBase
    {
        public string T2 { get; set; }
        public string T3 { get; set; }
    
        public strign O1 { get; set; }
    }
    

    There are many other ways. The last method above has the advantage of only once writing the logic for T1 and CompareTo, which saves from clutter and creates clarity in your code.

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

Sidebar

Related Questions

I have two classes which both derive from the same parent: public class People{
I have 2 classes both inherit from the same interface. i want sometimes to
I have 2 classes within same package. Both classes have main method in them.
I have two classes generated by LINQ2SQL both from the same table so they
I have three classes; Classes A and B both reference class C . How
Both the MySQLi and MySQLi_STMT classes have an $insert_id property. If I am connected
Let's say I have two classes. Both have the same member fields (including the
I have a base class like this: class FooBase { public bool Do(int p)
The situation is this: You have two classes that both implement the same interface
I have developed some classes with similar behavior, they all implement the same interface.

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.