0

I'm not sure when exactly i should use set and get methods .My confusion comes from this example that I'm reading right now. we should make a program that make a few balls and throw them and prints the number of throwing.

this program contains of two public class "colors" and "Ball". the colors class determine the color of the ball and Ball class determine the size of the ball.

its colors class:

public class Colors
{
    private byte red;
    private byte green;
    private byte blue;
    private byte alpha;

    public Colors(byte red, byte green, byte blue, byte alpha)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.alpha = alpha;
    }

    public Colors(byte red, byte green, byte blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
        this.alpha = 255;
    }

    public byte GetRed()
    {
        return red;
    }

    public void SetRed(byte red)
    {
        this.red = red;
    }

    public byte GetGreen()
    {
        return green;
    }

    public void SetGreen(byte green)
    {
        this.green = green;
    }

    public byte GetBlue()
    {
        return red;
    }

    public void SetBlue(byte blue)
    {
        this.blue = blue;
    }

    public byte GetAlpha()
    {
        return alpha;
    }

    public void SetAlpha(byte alpha)
    {
        this.alpha = alpha;
    }

    public byte Grayscale(byte red ,byte green, byte blue)
    {
       return (byte)((red + blue + green) / 3);
    }
}

and this is ball class

public class Ball
{
    private float radius;
    private int timesThrown;

    public Ball(Color color, float radius)
    {
        this.color = color;
        this.radius = radius;
        this.timesThrown = 0;
    }

    public void Pop()
    {
        radius = 0;
    }

    public void Throw()
    {
        if (radius > 0)
        {
            timesThrown++;
        }
    }

    public int GetTimesThrown()
    {
        return timesThrown;
    }
}

my question is that why we should use "set" and "get" method in first example? isn't constructor enough ? and why we didn't use set and get in ball class?for example for radius ?

Gabor
  • 3,021
  • 1
  • 11
  • 20
mohsen doraghi
  • 59
  • 2
  • 10
  • The example code you are reading probably originates from Java and is not a good C# code. See @Kane's answer below. – Olivier Jacot-Descombes May 08 '16 at 17:25
  • 4
    Maybe a duplicate of: http://stackoverflow.com/questions/5366232/when-to-use-get-set-in-c-sharp or http://stackoverflow.com/questions/5321698/when-to-use-get-and-set-properties-in-c-sharp – granmirupa May 08 '16 at 17:28

4 Answers4

3

Firstly you can replace your GetXXX and SetXXX methods with properties as methods are typically used to perform some type of function.

public byte Alpha { get; set; }

Setting properties through a constructor is a good way to set your object to a predefined state. But what if you need to update your properties after it has been initialized? You will need to call the properties set method

Kane
  • 16,471
  • 11
  • 61
  • 86
3

It seems that you just started to learn Object-Oriented-Design and C# is fully Object-Oriented. When students ask me why we use get\set (accessors and mutators are the formal definition for get and set) I tell them that they used to provide control of usage of this variables.

For example:

You are working in the shop and have access to an account-machine. You can proceed the payment. So, you have GET to variables of INCOMES. But you are NOT the account-chief, so you CAN'T SET variables related to TAX_RATIO.

Or if somebody makes a payment you want to inform the manager, so you could implement another method when GET is called. Look:

public double PaymentSumm { get { InformManagerCodeHere() } private set; }

The main aim of get\set is to provide control of usage and control of access\change level.

pro-gramer
  • 166
  • 1
  • 3
  • 14
Iskander Raimbaev
  • 1,322
  • 2
  • 17
  • 35
0

The various benefit of using getter and setter are mostly in "architectural" point of view, especially when you want to make some changes to the code later. You can refer to this link for the benefits: Why use getters and setters?

The radius is set to be private, without any getter or setter to change / read it as the developer thinks that it should not be exposed to other classes.

Community
  • 1
  • 1
0

Given example if confusing I must admit that. Here are some reasons

Encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.

Hiding the internal representation of the property while exposing a property using an alternative representation.

Insulating your public interface from change - allowing the public interface to remain constant while the implementation changes without affecting existing consumers.

Controlling the lifetime and memory management (disposal) semantics of the property - particularly important in non-managed memory environments (like C++ or Objective-C).

Getters and setters can allow different access levels - for example the get may be public, but the set could be protected.

How To Use Them and why

lets say you have a Price of an item

Class item
    {
     private int price=50;

         public int getprice()
        {
         return price; // you can use this fuction to retrieve the price of any item throughout your program
        }

        public int setprice(int newprice)
        {
         price newprice; // you can use this fuction set a new value to the price whenever you want. As you can see in start the price of item is 50 if you want to change it late you can simply call a function and set it. 
        }
}

Getters and Setter provide an encapsulated environment where users can not access the private members of a class directly but they have to follow a specific defined path (defined by you) with some restrictions they have to follow. like in a setter they can only set a value that is integer.

Ali Mohsan
  • 326
  • 2
  • 15