Tuesday, August 26, 2014

Getter and Setter in C# Unity3d

C# Unity has support for Getter and Setter properties which is very important in terms of Object Oriented Programming, Here is a simple example of Getter and Setter properties which shows its usefulness,
Without getter and setter properties:
  1. public int getX()
  2. {
    1. return x;
  3. }
  4. public void setX(int newX)
  5. {
  6. if(newX < 0)
  7. x = 0;
  8. else
  9. x = newX;
  10. }
To change the value of x you have to write like
  1. setX(getX()+4);
With getter and setter properties:
  1. public int X
  2. {
  3. get { return x; }
  4. set
  5. {
  6. if(value < 0)
  7. x = 0;
  8. else
  9. x = value;
  10. }
  11. }
Now in this case you can easily change value of x with 
  1. X += 4;
Getter and Setter is very usefull properties of C# programming language and can be utilize interestingly in Game Development also in Unity3d.

To know some basic but important key points about C# programming language go HERE,


Some basic use of these properties listed below,

1. Flexible way to get and set values conditionaly.
2. Works great with Unity3d Transform and other Vector properties.
3. Use mainly in Score/Health calculation/computaion etc.

No comments:

Post a Comment

Popular Posts