Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Property vs Variable?

Property vs Variable?

Scheduled Pinned Locked Moved C#
questionvisual-studio
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T That Asian Guy

    I've been using properties for quite some time, and it was until today that a question came to me: what's the difference between using a property and a variable? I know properties are better practice, but still... just wondering :).

    public int Number
    {
    get
    {
    return this.number;
    }
    set
    {
    this.number = value;
    }
    }

    vs

    public int number = 0;

    D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #4

    There are many reasons - a couple to get you started... 1. Using a property enables you to do validation etc within the object that holds the variable in the getter and setter methods. Imagine if the number variable should be restricted to a certain range under certain situations. If you expose the variable directly you have no control over what goes into it - other than the data type. With a property you can test the value parameter, and raise an exception or set the variable to a different value etc. 2. Properties do not have to refer to an actual variable. e.g.

    private int number1;
    private int number2;

    public int Sum
    {
    get { return number1 + number2; }
    }

    Dave
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

    T 1 Reply Last reply
    0
    • D DaveyM69

      There are many reasons - a couple to get you started... 1. Using a property enables you to do validation etc within the object that holds the variable in the getter and setter methods. Imagine if the number variable should be restricted to a certain range under certain situations. If you expose the variable directly you have no control over what goes into it - other than the data type. With a property you can test the value parameter, and raise an exception or set the variable to a different value etc. 2. Properties do not have to refer to an actual variable. e.g.

      private int number1;
      private int number2;

      public int Sum
      {
      get { return number1 + number2; }
      }

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

      T Offline
      T Offline
      That Asian Guy
      wrote on last edited by
      #5

      I see... thanks.

      1 Reply Last reply
      0
      • T That Asian Guy

        No, whats that :S?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #6

        If you don't 'need' the private variable yourself but are just using it to hold a property's value with no validation then you can do this and the compiler takes care of it all for you. Personally I never use them as I like to be in control of what's happening.

        // no need for private int number;
        public int Number { get; set; }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        T M 2 Replies Last reply
        0
        • T That Asian Guy

          No, whats that :S?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          Auto-Implemented Properties

          1 Reply Last reply
          0
          • D DaveyM69

            If you don't 'need' the private variable yourself but are just using it to hold a property's value with no validation then you can do this and the compiler takes care of it all for you. Personally I never use them as I like to be in control of what's happening.

            // no need for private int number;
            public int Number { get; set; }

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            T Offline
            T Offline
            That Asian Guy
            wrote on last edited by
            #8

            What would be the initial value of the property?

            D 1 Reply Last reply
            0
            • T That Asian Guy

              What would be the initial value of the property?

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #9

              Whatever the intial value of the data type - 0 in the case of an int. All value types have an initial value. Reference types will be null (I believe - I haven't checked) as no instance has yet been created.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

              1 Reply Last reply
              0
              • D DaveyM69

                If you don't 'need' the private variable yourself but are just using it to hold a property's value with no validation then you can do this and the compiler takes care of it all for you. Personally I never use them as I like to be in control of what's happening.

                // no need for private int number;
                public int Number { get; set; }

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #10

                I like these, simple in the above style and can easily be expanded to a normal property if required.

                Never underestimate the power of human stupidity RAH

                D 1 Reply Last reply
                0
                • T That Asian Guy

                  I've been using properties for quite some time, and it was until today that a question came to me: what's the difference between using a property and a variable? I know properties are better practice, but still... just wondering :).

                  public int Number
                  {
                  get
                  {
                  return this.number;
                  }
                  set
                  {
                  this.number = value;
                  }
                  }

                  vs

                  public int number = 0;

                  M Offline
                  M Offline
                  Mark Churchill
                  wrote on last edited by
                  #11

                  A property can be an interface member, whereas a variable can't be. This means changing the definition of a class to add/remove a property is a breaking change. Generally speaking if "Number" is part of the publically available API, then you want to expose it as a property. A variable can be passed by reference (ref/out), a property can't be. A variable has only one access modifier, a property can have a different modifier on get/set. In terms of overhead, the effort of implementation is quite small if you use automatic properties. int Foo { get;set; }. In terms of performance the simple accessor will usually be inlined by JIT, making the performance identical (except on forms - they inherit MarshalByRef). Also as a property is effectively a stub of metadata pointing to getter and setter methods, they can benefit from things like declarative security, etc.

                  Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                  Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                  1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    I like these, simple in the above style and can easily be expanded to a normal property if required.

                    Never underestimate the power of human stupidity RAH

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #12

                    For future extensibility they are useful I suppose - but I've found I nearly always need more control so I do it the 'old fashioned' way! Also, not being able to have true read only properties this way (you have to have a set; - although it can have a private accessor) is a pain.

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                    M J 2 Replies Last reply
                    0
                    • D DaveyM69

                      For future extensibility they are useful I suppose - but I've found I nearly always need more control so I do it the 'old fashioned' way! Also, not being able to have true read only properties this way (you have to have a set; - although it can have a private accessor) is a pain.

                      Dave
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #13

                      I use properties a LOT, I have a class for each table in my BLL and a property for each field in the table (all auto generated) so nearly all of these can be serviced by the auto property. Expanding an auto to a full property is really simple and can be done at any time.

                      Never underestimate the power of human stupidity RAH

                      1 Reply Last reply
                      0
                      • D DaveyM69

                        For future extensibility they are useful I suppose - but I've found I nearly always need more control so I do it the 'old fashioned' way! Also, not being able to have true read only properties this way (you have to have a set; - although it can have a private accessor) is a pain.

                        Dave
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                        J Offline
                        J Offline
                        J4amieC
                        wrote on last edited by
                        #14

                        you know you can do this: public int MyInt{get; **private** set; }

                        D 1 Reply Last reply
                        0
                        • J J4amieC

                          you know you can do this: public int MyInt{get; **private** set; }

                          D Offline
                          D Offline
                          DaveyM69
                          wrote on last edited by
                          #15

                          Yeah - in my post...

                          DaveyM69 wrote:

                          although it can have a private accessor

                          Dave
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups