How to use a Class variable in a Method

Hi, I have created a class(Aircraft) that I’m using as a data holder (That stores strings, ints, and enums) and have made a List(Aircraft) of that class. I would like to write a method that returns a new list from List(Aircraft) with the specific variables from Aircraft and its values.

public class AircraftManger : MonoBehaviour
{
    public List<Aircraft> aircraftsList = new List<Aircraft>();

    public void AddAircraft(string aircraftName, int numberOfWheels, int numberOfWings, 
    Aircraft.EngineType engineType)
    {
        aircraftsList.Add(new Aircraft(aircraftName, numberOfWheels, numberOfWings, engineType));
        aircraftsList.Sort((a1, a2) => a1.aircraftName.CompareTo(a2.aircraftName));
    }
    public void RemoveAircraft(string name)
    {
        foreach (Aircraft a in aircraftsList)
        {
            if (a.aircraftName == name) aircraftsList.Remove(a);
        }
    }
    **public List<Aircraft> ReturnAircraftsWithName(string name) //Returns a list of <Aircraft> with nam 
    e.
    {
        List<Aircraft> returnList = new List<Aircraft>();

        foreach (Aircraft a in aircraftsList)
        {
            if (a.aircraftName == name) returnList.Add(a);
        }

        return returnList;
    }
    public List<Aircraft> ReturnAircraftsWithNumberOfWheels(int numberOfWheels) //Returns a list of 
    <Aircraft> with number of wheels.
    {
        List<Aircraft> returnList = new List<Aircraft>();

        foreach (Aircraft a in aircraftsList)
        {
            if (a.numberOfWheels == numberOfWheels) returnList.Add(a);
        }

        return returnList;
    }**
}

public class Aircraft //Data Holder class
{
    public string aircraftName;
    public int numberOfWheels;
    public int numberOfWings;
    public enum EngineType {Piston, Turbine, Electrical}
    public EngineType engineType;

    public Aircraft(string aircraftName, int numberOfWheels, int numberOfWings, EngineType 
    engineType)
    {
        this.aircraftName = aircraftName;
        this.numberOfWheels = numberOfWheels;
        this.numberOfWings = numberOfWings;
        this.engineType = engineType;
    }
}

Now, instead of having to continue with the pattern of creating similar methods like ReturnAircraftsWithName, ReturnAircraftWithNumberOfWings, ReturnAircraftWithNumberOfWheels (Creating the same method for each variable in the Aircraft class) I would like to have a shorthand version of this. Something like:

    public List<Aircraft> ReturnAircraftsWithProperty(VariableName value) 
    {
        List<Aircraft> returnList = new List<Aircraft>();

        foreach (Aircraft a in aircraftsList)
        {
            if (a.Variablename == value) returnList.Add(a);
        }

        return returnList;
    }

So that I use a method like this to return a list Aircraft -

List<Aircraft> biplanes = ReturnAircraftsWithProperty(Aircraft.numberOfWings(2));

My actual code has an Aircraft class with over 20 variables so I don’t want to write 20 methods that are basically the same.

This is a great use case for LINQ. A set of extensions for collection types that allows easy manipulation and querying of collection data.

For example, if you want to have a list of all aircrafts with 2 wings, then:

using System.Linq;

List<Aircraft> aircraftsWithTwoWings = aircraftsList.Where(aircraft => aircraft.numberOfWings == 2).ToList();

That is all you have to do. And you can use the same pattern to produce lists of any kind of query.

LINQ is probably the easiest solution for this but would not be my favorite tbh. In general i still want to advise to not use LINQ where possible as it often has a heavy performance impact compared to basic for loop solutions.

I imagine that it might also be possible to create some function to the tune of:

public List<Airplane> getPlanesWithCondition(System.Func<Airplane, bool> comparefunc)
{
    foreach(var airplane in allAirPlanes){
         if(comparefunc.Invoke(airplane))
            //add...
    }
}

//in some code:
var conditionalList = getPlanesWithCondition((Airplane plane) => {return plane.winCount==2});

which has a similar notation and should work without linq.
Not sure if that really is faster/better though. Also not testet and somewhat pseudocode.