public class Infrastructure
{
public string ID; //ID of the building
public string buildingName; //Name of the building
public int buildingType; //Type of the building (0 = HQ, 1 = Research, 2 = Analytics)
public int buildingLevel; //Level of that type of building
public int cost; //How much money does construction of this building cost
public int time; //How much time it need to complete construction of builing
public int upkeepCost; //How much you need to pay monthly after completing construction
public int requiredTech; //What technologies need to be researched to build this building
public int requiredBuilding; //What buildings need to be constructed to construct this building
public int image; //Index of image of building
public Infrastructure(string inputID, string inputName, int inputType, int inputLevel,
int inputCost, int inputTime, int inputUC)
{
ID = inputID;
buildingName = inputName;
buildingType = inputType;
buildingLevel = inputLevel;
cost = inputCost;
time = inputTime;
upkeepCost = inputUC;
requiredTech = -1;
requiredBuilding = -1;
image = 0;
}
}
public List<Infrastructure> buildings;
i have buildings list that store x amont of classes infrastructure. In infrastructure class there are variables. On List variable i can use list.Contains(varaible) and returns true or false if list has that variable.
Is there a way to use something similair on class? like classInfrastructure.ID.contains(“some string”), for this case does any class from buildings list contains Infrastructure class with ID variable that is “some string”.
Equivalent for the list would be ListID.contains(“some string”)
If you want to simply find out if the building with field that equals to something exists in the list, you can do the following:
List<Infrastructure> yourList = ...
// Some id
string checkId = "Id";
// Returns true if any Infrastructure in list with Id equal to checkId exists. False otherwise
bool hasAny = yourList.Any(x => x.ID.Equals(checkId));
You can also iterate over the list using foreach to achieve same result:
private bool HasIdInList(string id){
foreach (Infracture building in someList) {
if (building.Id.Equals(id)) return true;
}
return false;
}
Note that if you’re planning to access items by id’s, it’s better to store them into Dictionary.
Such as Dictionary<string, Infrastructure>;
That way you can easily retrieve them, or check if they’re exist in the collection.
Not home but i think that is what i have been looking for. Basically infrastructure class has string ID and when i add new class i need to check if class with same ID exists. Classes are imported from external xml
I will check Dictionary method but i think it wont be easier than list of classes. Sometimes i need for loop with specific place and also i have quite the amount of scripts set up with this system.
With Dictionary approach, you’ll get O(1) check speed, where as List iteration would be O(N), which is slower.
Iterating over list each time you need to check if id exists would be really slow, once that list grows.
Same applies to the retrieval of the item.
.ContainsKey(id) would provide you with an easy check, retrieval can be easily be implemented using .TryGetValue()
TL;DR: It would much more faster to store a dictionary of existing id’s.
If you don’t need the access via id, you can simply store id’s in a HashSet, which provides same O(1) speed, and same .Contains(id).
Both can be updated when you’re loading from external storage.