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
  1. Home
  2. General Programming
  3. C#
  4. Question about properties

Question about properties

Scheduled Pinned Locked Moved C#
questioncsharplearning
5 Posts 5 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.
  • X Offline
    X Offline
    xdavidx
    wrote on last edited by
    #1

    I was just in Barnes and Noble and I was skimming through the book "Effective C#" by Bill Wagner. One of his tips was that you should never have a public variable in a class, that you should ALWAYS use properties. This sorta goes against some things that I've read in other books. I was just curious what others thought about it. Isnt there extra overhead for using properties?

    C A J 3 Replies Last reply
    0
    • X xdavidx

      I was just in Barnes and Noble and I was skimming through the book "Effective C#" by Bill Wagner. One of his tips was that you should never have a public variable in a class, that you should ALWAYS use properties. This sorta goes against some things that I've read in other books. I was just curious what others thought about it. Isnt there extra overhead for using properties?

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

      xdavidx wrote: One of his tips was that you should never have a public variable in a class, that you should ALWAYS use properties. This sorta goes against some things that I've read in other books. What other books?! I've never seen that advice. Bill Wagner is right - you should use properties. You should declare all member variables as private or protected (some people say that even protected is bad - But I think the jury's still out on that one) Declaring a member variable as public means that encapsulation is broken and anything outside the object can modify the member variables without notice. Using a property you can control what happens. Sometimes you don't mind that the member variable is updated from outside as any valid value cannot make the state of the object invalid. In this case the property just passes through the value and assigns it to the member variable. What is the point in that? (you might ask.) Well, if in the future you realise that when that variable changes something else must happen then you have your property already set up and adding some lines of code to the property means that you can do the other work without having to modify vast amounts of code to now use the property instead of a member variable. Sometimes you might only want other code to have read-only access to the member variable's value, in this case you only implement a getter property so that nothing can set values on the property. Does this help?


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

      1 Reply Last reply
      0
      • X xdavidx

        I was just in Barnes and Noble and I was skimming through the book "Effective C#" by Bill Wagner. One of his tips was that you should never have a public variable in a class, that you should ALWAYS use properties. This sorta goes against some things that I've read in other books. I was just curious what others thought about it. Isnt there extra overhead for using properties?

        A Offline
        A Offline
        Andy Moore
        wrote on last edited by
        #3

        I personally think that you should use properties since you can hide your data members from clients of your class. This enforces a better object-oriented design. Also, if you want to expose your .NET classes to COM clients, then you must use properties. Properties are really function calls so the only overhead to using them is function call overhead. Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons

        S 1 Reply Last reply
        0
        • A Andy Moore

          I personally think that you should use properties since you can hide your data members from clients of your class. This enforces a better object-oriented design. Also, if you want to expose your .NET classes to COM clients, then you must use properties. Properties are really function calls so the only overhead to using them is function call overhead. Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          The JIT compiler might even inline the function calls when calling properties, as most property get/setters don't do much. That'll do away with the function call overhead also. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          1 Reply Last reply
          0
          • X xdavidx

            I was just in Barnes and Noble and I was skimming through the book "Effective C#" by Bill Wagner. One of his tips was that you should never have a public variable in a class, that you should ALWAYS use properties. This sorta goes against some things that I've read in other books. I was just curious what others thought about it. Isnt there extra overhead for using properties?

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            One of the C# program managers, Eric Gunnerson[^], has commented that in some cases properties are over-used. There are only a few times when I don't write properties out, mainly when dealing with structs or classes that are used just as data storage without any methods. For example, one easy way to implement IEditableObject is to do something like this:

            class Foo : IEditableObject
            {
            private struct FooData
            {
            public int i;
            public string j;
            public DateTime t;
            public object o;
            }

            private FooData data;
            private FooData old;

            public int I
            {
            get { return data.i; }
            set { data.i = value; }
            }

            public string J
            {
            get { return data.j; }
            set { data.j = value; }
            }

            // Repeat for all members exposed

            void BeginEdit()
            {
            old = data; // FooData is a value type so this is making a copy of data
            }

            void CancelEdit()
            {
            data = old; // Copy the old values back
            }

            void EndEdit()
            {
            // Nothing to do
            }
            }

            I believe I do something similar in my IExtenderProvider article where all of the data stored for each object 'extended' is stored in a class with public fields. I know this is the case in the code that inspired the article. In both cases the type exposing public fields is only intended for the use of one class in a private manner. James

            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