Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by NikEy · Dec 30, 2013 at 01:57 AM · class objectgeneralsettersgetters

Getter and Setter in C#

I've got this in one class (which is then initialized in the constructor):

 public Dictionary<int, List<Obj>> objectsBySystem { get; private set; }

My hope was that this restricts the variable to the internal scope only, such that values cannot be set from outside the class.

Unfortunately, testing this from an outside class, it seems that these values can still be set:

 print(ObjectManager.instance.objectsBySystem.ContainsKey(123));
 //yields False

 ObjectManager.instance.objectsBySystem.Add(123, new List<Obj>());
 print(ObjectManager.instance.objectsBySystem.ContainsKey(123));
 //yields True


Any advice? Thanks!

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

2 Replies

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

Answer by steakpinball · Dec 30, 2013 at 02:59 AM

Specifying private set; only makes setting the dictionary itself to private. You can not do

 ObjectManager.instance.objectsBySystem = new Dictionary<int, List<Obj>>();

outside of the class.

If you want to limit calling of methods you should make objectsBySystem private.

 private Dictionary<int, List<Obj>> objectsBySystem;


A basic solution provides intermediate methods for funtionality you want to access. An example would be a basic getter.

 public List<Obj> GetObjectsBySystemNumber(int number)
 {
     return objectsBySystem[number];
 }


You also have the option of using an indexer for basic getting and setting of values instead of methods. http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

 public List<Obj> this[int number]
 {
     get { return objectsBySystem[number]; }
     private set { objectsBySystem[number] = value; }
 }
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 NikEy · Dec 30, 2013 at 03:50 AM 0
Share

the indexing part was new to me, that's quite interesting. thanks

avatar image
0

Answer by KellyThomas · Dec 30, 2013 at 03:00 AM

This:

 ObjectManager.instance.objectsBySystem.Add(123, new List<Obj>());

Is equivalent to:

 List<Obj> list = ObjectManager.instance.objectsBySystem;
 list.Add(123, new List<Obj>())

so you are only using the getter, however the Dictionary is mutable and as such changes can be made once it is handed to the other class.

Microsoft have some support for an `ImmutableDictionary` but it is at .NET 4.5 and not available for unity.

Depending on the properties of a Dictionary you value most you could implement a class that takes a copy of the key/value pairs and stores them as parallel ImmutableLists this would allow you to implement data look up but would not have the speed of a true hashmap.

The easiest (and probably fastest to execute) would be to encapsulate the Dictionary and provides accessor methods for the functionality you want to reveal.

 private Dictionary<int, List<Obj>> objectsBySystem = new Dictionary<int, List<Obj>> ();

 public bool ObjectsContainsKey(int key) {
     return objectsBySystem.ContainsKey(key);
 }

 public List<Obj> ObjectsGetValue(int key) {
     return objectsBySystem[key];
 }


 



 


Comment
Add comment · 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

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

20 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

Related Questions

Get and Set difference 2 Answers

How can I display an object with a script based off an int in a different script? 1 Answer

problems with setters and getters. 1 Answer

Abstract Inheritance and abstract variables sloppy code 1 Answer

getter and setter not working 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