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 raed · Nov 27, 2011 at 02:45 PM · c#scriptingbasics

What is setter and getters??,What is setter and getters

hi, i wanna ask about setter and getters what are they used for and an example of setter and getter

and thanks,,hi, can u give me information about setter and getters like in C# 1_ why we use thim 2_ example of this method and explan

and thanks,

Comment
Add comment · Show 2
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 syclamoth · Nov 27, 2011 at 03:11 PM 1
Share

Seriously, Google is your friend.

avatar image Kerihobo · Oct 21, 2014 at 12:45 AM 1
Share

google brought me here.

3 Replies

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

Answer by ptdnet · Nov 27, 2011 at 05:19 PM

They are public functions that can be called to GET or SET the value of a private variable.

 public myclass {
   private int _score;

   public int MyScore {
      get { return _score; }
      set { _score = value; }
   }
 }

So in the above example you can access MyScore instead of _score. Totally useless and a complete waste of time and speed in my opinion, but object oriented purists love them.

Comment
Add comment · Show 5 · 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 aldonaletto · Nov 27, 2011 at 05:34 PM 1
Share

On the contrary, the concept of properties (which include setter and getter) is extremely useful - not in your example, of course, but when you need to do additional things when setting a variable value. The "variables" transform, eulerAngles, rotation, rigidbody etc. are actually properties, and have getters and setters way more complex than simple assignments - take a look at my answer.

avatar image WillTAtl · Nov 27, 2011 at 07:21 PM 0
Share

I actually dislike them in this syntactical form, even when they do useful things. To me it just obfuscates things to have code that "magically" happens when by all appearances you just set a member variable, and I always prefer to have explicit GetVariable() and SetVariable(value) methods in any case where something other than simply getting/setting the value actually goes on under the hood, so people using the code later can instantly say "aha, I'm not just setting a variable, I'm calling a function which will execute with potential side-effects!"

avatar image raed · Nov 27, 2011 at 07:21 PM 0
Share

is value = to the original value of the variable _score in set{_score = VALUE; }

avatar image Peter G · Nov 27, 2011 at 07:27 PM 0
Share

value is the value being assigned to the property. Its a reserved word.

 $$anonymous$$yScore = 5;

here value is 5. Its whatever is on the right hand of the assignment operator. The setter would translate into:

 _score = 5;
avatar image Magnomous · Aug 07, 2013 at 05:29 PM 0
Share

Thank you pdnet, by this you saved me from C# rage quit.

avatar image
3

Answer by Peter G · Nov 27, 2011 at 06:37 PM

I'm probably not going to say too much that hasn't already been said, but I'm going to add a few things. Properties are function calls for getting and setting a value. They are most useful for abstraction. Properties are an easy way of hiding implementation from other classes. @Aldonaletto gave a good example of when you would do this. Another good one is getting the length of a vector.

   public float magnitude {
           get {
                 return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
           }
   }

This example provides 2 good reasons why to use properties:

  1. Other classes can't see how we get the magnitude which is good b/c they don't need to know. They only need the final value. Abstraction.

  2. It only has a getter. A getter means that other objects that access this can only read our value, they can't tell us to change anything. This is used in the lazy loading frequently. There are also properties with only setters.

If you use properties, its important to be consistent. Properties and variables are similar, but there not the same. Someone trying to figure out your code later will probably thank you for being consistent, especially if they are reflecting over it. A useful feature when the property would be a simple getter/setter:

   public float Value {
         get; set;
   }

Another useful feature of properties is that you can put them in interfaces and abstract classes. This is an advantage they have to variables. My favorite example:

 public abstract class Animal {
          public abstract int Limbs { get; }
 }
 public class Dog : Animal {
         public override int Limbs {
                 get {return 4; }
         }
 }

This way you can get the number of limbs on every animal and know that it is a safe operation.

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
avatar image
1

Answer by aldonaletto · Nov 27, 2011 at 05:57 PM

One of the most interesting OOP ideas is the concept of property. A property looks like an ordinary variable, but the big difference is the use of getter and setter, routines that are internally called when you read or assign to a property. Most GameObject "variables" are actually properties, each one with its own getter and setter. In most cases, there exists an ordinary private variable that stores the property value, but the getter and setter do a lot of extra things when you read or assign to the property.
The parent variable, for instance, is a property. Probably the getter is a simple routine like in the @ptdnet example, that only returns the value of the private _parent (or whatever they named it) variable. The setter, on the other hand, is way more complicated: when you assign a transform to the parent variable, the setter must calculate and assign localPosition, localRotation and localScale, not to mention several other internal changes related to physics or to the game object structure. If it wasn't a property, you should do all this extra work by hand, instead of just assigning the value to parent (thanks God for properties!)
Most times, the getter is simpler than the setter, but not always: eulerAngles, for instance, is a completely virtual property; there's no such a thing like a private variable Vector3 _eulerAngles - the getter converts the rotation internal value to Euler angles, and returns a Vector3. The declaration of the property eulerAngles probably is something like this:

  public Vector3 eulerAngles {
     get { return ConvertRotationToEuler(_rotation); }
     set { _rotation = ConvertEulerToRotation(value); }
  }
Where the functions ConvertRotationToEuler and ConvertEulerToRotation are very complex routines that run behind the scenes.
If you just need to assign to a variable or read its contents, don't mind - like @ptdnet said, using setter/getter in this case is a complete waste of time. But if you need to do other things anytime this variable is modified or read, then a property is the answer.
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 DragonBorn007 · Jul 31, 2014 at 08:08 PM 0
Share

I don't understand. Can't you return the result of a function call inside a regular function also?

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

9 People are following this question.

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

Related Questions

How to create a Graph in Unity 1 Answer

Does Unity Script syntax change from release to release? If so how much? 4 Answers

Can't use any variable from different class/ NullReferenceExcept 0 Answers

Distribute terrain in zones 3 Answers

Screen.width ~ HealthBar problem - C# 2 Answers


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