Is having a List in an interface increasing overhead
Hello everyone,
Sorry if I'm not understanding this problem correctly, but I'm new to C#. So I have two interface class called IArmy and IUnit. In IArmy is a List of IUnit classes with a get keyword. (So an army contains a list of units)
In the implementation of IArmy, I have a List of Unit classes
public class Army : MonoBehaviour, IArmy
{
List<IUnit> mUnitList;
public List<IUnit> UnitList
{
get
{
return mUnitList;
}
}
// Rest of the code
}
What I'm confused about is that I have UnitList and mUnitList. Is UnitList acting like a pointer to mUnitList, or do I have two lists thus creating extra memory by accident? I’m used to C++ pointers so I’m not 100% if what I’m doing is correct.
Thank you very much!
Answer by HenryStrattonFW · Nov 06, 2015 at 12:06 PM
What you are using here is typically called an Accessor, or Property, and no it does not mean you have two lists. Nor is it acting like a pointer, the get property (and the set property) act more like functions just like writting.
public List<IUnit> GetUnitList()
{
return mUnitList;
}
Propertys allow you to do various things like validating values that are being set to the variable, abstract out the selection of a value to return from a pool of possible options, etc.
You don't have to worry about extra memory overheads here. Just the processing overhead of any logic you put inside your get and set properties. which in this case is negligible as its just a return.
Thank you for the very clear answer! I find doing these in interface classes very handy.
Your answer
Follow this Question
Related Questions
How Do I Create a List of Two Strings? C# 1 Answer
List keeps losing items. 0 Answers
Updating a List from a Custom Inspector 0 Answers
Firebase List Users 1 Answer