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. what is the difference between Property and Public Field?

what is the difference between Property and Public Field?

Scheduled Pinned Locked Moved C#
questioncsharphelp
7 Posts 5 Posters 8 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.
  • K Offline
    K Offline
    Khoa Bui
    wrote on last edited by
    #1

    I've been using C# for a long time. But one day, a friend ask me, what is the difference between them. I can't answer. So, can anybody help me answer this little question?

    I A E 3 Replies Last reply
    0
    • K Khoa Bui

      I've been using C# for a long time. But one day, a friend ask me, what is the difference between them. I can't answer. So, can anybody help me answer this little question?

      I Offline
      I Offline
      iliyang
      wrote on last edited by
      #2

      Well, it is the one magical thing that grabbed me a few years ago when I was reading a thin book about the C# language ;) The public field is just a variable that you can read and write without any control. I mean that you can set the field any value that is possible for the type of the field. And that's pretty much it. While the property is a more complex thing - it appears to be a public variable but it's a totally different thing. A property is a set of one or two functions in fact (accessor and/or modificator). For example, assume that we have the "public string Name" property. When the compiler generates the IL code, it generates also one or both of the following functions: public string get_Name() { the_statements_in_the_get_function; } public void set_Name(string value) { the_statements_in_the_set_function; } And you can do anything inside. Getting the value of a property calls the get_Name function, and setting a value to the property calls the set_Name function. That's pretty powerful, 'cause you can do many more things than return/set the actual variable beneath (if there is one) - you can raise events, force redraw etc. And of course, we should thank Delphi for this nice language feature ;) Cheers!

      1 Reply Last reply
      0
      • K Khoa Bui

        I've been using C# for a long time. But one day, a friend ask me, what is the difference between them. I can't answer. So, can anybody help me answer this little question?

        A Offline
        A Offline
        Andrew Kirillov
        wrote on last edited by
        #3

        Hello If you'll look at COM, you will see, that defining a property for an object leads to appearing of two functions: get_PropertyName and put_PropertyName. In C++ you will use these functions to access the property value, in VB you can work with the property "directly" (obj.PropertyName). In C# is similar. There are two accessors set and get. And you can perform whatever calculation you want inside them. Andrew

        K 1 Reply Last reply
        0
        • A Andrew Kirillov

          Hello If you'll look at COM, you will see, that defining a property for an object leads to appearing of two functions: get_PropertyName and put_PropertyName. In C++ you will use these functions to access the property value, in VB you can work with the property "directly" (obj.PropertyName). In C# is similar. There are two accessors set and get. And you can perform whatever calculation you want inside them. Andrew

          K Offline
          K Offline
          Khoa Bui
          wrote on last edited by
          #4

          :)

          C 1 Reply Last reply
          0
          • K Khoa Bui

            :)

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Actually, the two definitions that you got missed off something very important. You should never make a field public because it blows a great big hole the encapsulation aspect of OO. Even if you just want direct access to a field in a class, you should wrap it in a property in order to future proof it against possible changes. At the moment you may only need to set the field underneath the property. But in future you may need to tell other objects about the change. If you were just accessing the field directly then you couldn't add this functionality easily.


            My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

            I 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Actually, the two definitions that you got missed off something very important. You should never make a field public because it blows a great big hole the encapsulation aspect of OO. Even if you just want direct access to a field in a class, you should wrap it in a property in order to future proof it against possible changes. At the moment you may only need to set the field underneath the property. But in future you may need to tell other objects about the change. If you were just accessing the field directly then you couldn't add this functionality easily.


              My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

              I Offline
              I Offline
              iliyang
              wrote on last edited by
              #6

              Yes, yes! That's very important! Never expose a field publically unless it is a readonly or const. But you even can do it for them too. The only case that I use public fields in, is when deriving from EventArgs. I put some data as public readonly fields to reduce the code and to speed up the handling a little bit. And if you look up the FCL reference, there's almost no public fields exposed, unless constants such as EventArgs.Empty, String.Empty and so on.

              1 Reply Last reply
              0
              • K Khoa Bui

                I've been using C# for a long time. But one day, a friend ask me, what is the difference between them. I can't answer. So, can anybody help me answer this little question?

                E Offline
                E Offline
                eggie5
                wrote on last edited by
                #7

                A property is like creating a whole method for manipulating a field. /\ |_ E X E GG

                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