Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Spartiate0033 · Nov 05, 2015 at 10:34 PM · c#list

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spartiate0033 · Nov 06, 2015 at 09:35 PM 0
Share

Thank you for the very clear answer! I find doing these in interface classes very handy.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to remove items in a list based on if the player object is not in a collider object that has the same assigned number variable as the list item 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges