public vs get, set

This is a discussion on public vs get, set within the C# Programming forums, part of the General Programming Boards category; Hello, I'm coding some C# and cant really get the difference between public and the properties get, set. In me ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    15

    public vs get, set

    Hello,

    I'm coding some C# and cant really get the difference between public and the properties get, set. In me world the two things is kinda the same but my guess is that I have missed something so any help would be nice ty.

    Example:

    Code:
    class TheNumber
    {
         public int number;
         public int Number {get; set;}
    }
    Then from any part of the code I can write:

    Code:
    TheNumber theNumber = new TheNumber();
    
    theNumber.number = 10;
    theNumber.Number = 10;
    so why cant I just have a public member instead of the property one in this case?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    get set can be methods. You can do more than just setting the variable.

    Code:
    int num_div_2;
    public int Number {
         get {
              return num_div_2 * 2;
         }
         set {
              num_div_2 = value / 2;
         }
    You can also only define one of them to make the property read-only or write-only.

    There may not even be a variable associated with the attribute.

    For example, for a linked list, you can have a read-only attribute Length that goes through the list to calculate the length, but to the user it would just look like a variable.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can make immutable objects (objects you cannot modify once created):
    Code:
    public class Location
    {
      public Location(int X, int Y)
      {
        this.X = X;
        this.Y = Y;
      }
    
      public int X { get; private set; }
      public int Y { get; private set; }
    }
    
    var MyLocation = new Location(1, 2);
    var MyX = MyLocation.X; //Ok
    MyLocation.X = 3; //Invalid
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The backing store for the property is not needed. The compiler will create one for you.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Of course, if you don't want to use anything more sophisticated you can turn them public. Personally I use public variables for personal projects because I am lazy. A good practice is to use set/get for flexibility. Maybe you want to add something later. In C++ that could be a must. In C#, though, you use the same syntax. So I guess if you wanted to use a get/set you can just create one without change your whole code.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Another reason to use properties instead of public variables:

    Properties help you follow the better programming practice of "program to an interface, not an implementation". With a property, you can alter the get/set accessors at will to make changes to the class functionality without changing any code outside of the class. The world outside of your class doesn't need to know -- or care -- how that property is internally stored or calculated. As long as the new class has the same interface as the old, it should be a direct substitute.

    Your class should be a 'black box' to the rest of the code -- the code using your class should know everything about how to use the class (the interface) but nothing about how the class works internally (the implementation).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There are of course some downsides. You might get tempted to make your code very safe decreasing performance drastically. A simple example is when you have an Execute() logic. You pass some parameters on you object and then you want to change the object with an Execute() command. You might put a get/set on every parameter, which is fine.
    But you might go too far and put the Execute() command everytime you set it. You idea is to make sure that the changes are reflected on the object. But then you might have some code like:
    Code:
    obj.height = 10;
    obj.x = 1;
    obj.y = 2;
    obj.color = Color.Red;
    ...
    and everytime the Execute() command will be called, when you could call it only when you are done passing parameters. If the Execute() is very costly, your program might get too slow. The problem with the above is that the code might indeed be a "black box" so you won't know that Execute() is called all the time. So using get/set by itself is fine, but that doesn't imply that you should not provide the basic mechanisms on simply being able to assign variables if that is what you really want in your program.
    So, you need some planning when using a get/set.

    I believe the idea is mostly intended for code that will be re-used. Otherwise it is more or less a preference the way you program.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    If you only want to call Execute when you're done passing parameters, why not make those 'properties' arguments to the Execute method? Properties are intended to set object state, not to perform functionality. If the Execute is very costly, I'd argue that it doesn't belong in a property definition at all.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Many controls in winforms/asp.net use them specifically for data binding. While that doesnt mean squat in a discussion about core language features, it's vitally important on a discussion about why to use them. It is very correct to say that properties are more for the interface, while private/public variables are for the implementation.

    In designing libraries for other programmers at work, my typical use of properties includes:

    providing the expected hook for data binding
    providing a wrapper around a specific storage paradigm (viewstate, session, cache, db)
    providing the necessary access for initializer lists (again, a real keystroke saver)
    providing the necessary access for linq operators

    notice all of my uses are for the interface, not the implementation
    Last edited by StainedBlue; 06-02-2010 at 08:32 PM.
    goto( comeFrom() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 11:05 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. "public" public objects
    By VOX in forum C# Programming
    Replies: 6
    Last Post: 06-22-2005, 12:32 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21