Question about properties
-
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?
-
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?
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
-
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?
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
-
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
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
-
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?
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