Sarr Sarr - 5 months ago 20
C# Question

C# - How to get item from Generic List with the highest specific parameter?

I've searched a lot but can't find the answer that I understand enough to translate into my project. What is the goal: I need to find an item with the highest armor_class parameter on the list, and add that items' armor_class to character's armor class.

So, we have a list created this way:

public List<Weapon> characterInvWeapon;
public List<Armor> characterInvArmor;


Here's how class Armor and its properties are created:

public class Armor : Item, IComparable <Armor> {
public string armor_prof;
public string armor_category;
public int armor_class;
public int armor_str_req;
public string armor_stealth_mod;

public Armor (string c_name
, string c_description
, bool c_stackable
, int c_value
, string c_coin_type
, int c_weight
, string c_armor_prof
, string c_armor_category
, int c_armor_class
, int c_armor_str_req
, string c_armor_stealth_mod) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
armor_prof = c_armor_prof;
armor_category = c_armor_category;
armor_class = c_armor_class;
armor_str_req = c_armor_str_req;
armor_stealth_mod = c_armor_stealth_mod;

public int CompareTo(Armor other)
if (armor_class == other.armor_class)
return String.Compare (name,; // a < ab < b
return other.armor_class - armor_class;

Armor is a class inheriting from class Item, which has the first 6 properties. Armors are stored in a list specific to Armor -
public List<Armor> characterInvArmor;

Example item:

AddToItemStore(new Armor("Breastplate", "Description.", false, 400, "gp", 20, "Breastplate", "Medium Armor", 14, 0, ""));

Adding script:

public void AddToCharacterInventory(Item it)
if (it is Weapon)
else if (it is Armor)

Now as I mentioned, I need to find an item on the list charInvArmor with highest armor_class parameter and use its value in other function, which calculates armor class from many variables.

So in other function,
characterArmorClass = armorWithHighestArmorClass + otherVariable + someotherVariable;

I suspect there are some handy shortcuts in Linq, but I'd be most thankful for some example without Linq. Linq would be welcome too, but I'm absolutely new to it and also I'm worried about performance and my apps compatibility with iPhone, for example. I've read iOS can cause problems with Linq. This must be fast and compatible calculation.


You have to loop through the list and check each value since the list is not sorted based on the armor class value and you do not want to use LINQ:

int maxArmorClass = 0;
foreach (var armor in characterInvArmor)
    // Do a comparison here and see if you found a higher value
    // If a higher value is found, store it in maxArmorClass

As a side note I recommend the following links:

Public Fields versus Automatic Properties


Style guide for c# * In C#, Pascal casing and camel casing are the established conventions.