Click here to Skip to main content
15,746,076 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm a beginner in C#,
I created a list of objects with properties and I'm trying to compare a property between two objects of my list.

For example, i've got a list of car, with properties (wheel size, car color, car size) and I want to compare the size of the wheel for each car.

I hope my explanations are clear, i'm not perfectly bilingual so sorry if you don't understand my whole topic ; I can explain more if it's necessary.

Thanks a lot for your help.

What I have tried:

class MilestoneInFlight : Milestone
    public float MilestoneDistance { get; set; }
    public float MilestoneAltitude { get; set; }
    public string MilestoneName { get; set; }

    List<MilestoneInFlight> MilestoneInFlightList = new List<MilestoneInFlight>();

    public void AddMilestone(float dist, float alt, string name)

        MilestoneInFlight milestone = new MilestoneInFlight
            MilestoneDistance = dist,
            MilestoneAltitude = alt,
            MilestoneName = name,



    public void Deconstruct(out float dist)
        dist = MilestoneDistance;
        //So here I want to compare MilestoneDistance for each MilestoneInFlight object

Updated 27-May-20 5:30am

1 solution

Add a method you your car class that takes another car object as a parameter and then compares the wheel size, something like this:

public class Car
    public int WheelSize { get; set; }

    public IsWheelSizeEqual(Car otherCar)
        bool result = false;
        if (otherCar != null)
            result = (this.WheelSize == otherCar.WheelSize);
        return result;

You could make the method a lot more useful is you used reflection and allowed the calling method to specify which property to compare for equality.

You could also write an extension method which would compare all properties in the two car objects for equality.

I suspect the last two ideas above would far exceed what your instructor expects from a beginner C# programmer...
Share this answer
Mineodo68 28-May-20 3:29am    
Hello and thanks for your help.
The thing that I will have to do is to see if, for example, the n1 car wheel size is 5 inch bigger that ne n2 wheel size.
I suppose I can do some modifications on the code like :
result = (this.WheelSize == otherCar.WheelSize + 5)
Could it works ?
Thank you very much, I appreciate it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900