private int empID;
public int empID {
get { return empID; }
set { empID = value; }
}
My question is, isn't this useless and inefficient?
I can't see any benefit to this form of encapsulation over
just making the field public. Later, if you do want to add
some checking or whatever, you can just change the class
to make the field private and add the accessor/mutator
code w/o changing the code which uses the class.
The downside to using properties is efficiency. Every access
to this property is now a function call. It's also annoying
while single-stepping in the debugger, especially code like
"foo(joe.empID);" because you're forced to step into the
accessor code.
Is there some benefit that I'm missing, perhaps dealing
with inter-language issues?
Keith
Also, making a field public, means users can read and write to it. Where as
with a property, you can make it read only or write only - which can be very
important.
Yes, a property is only syntactic sugar for get/set methods. So the reasons
for using a property, are the same as those for having written the get/set
methods.
Additionally, using properties follows the .NET standard, properties will
have a specific icon pop up in intellisense. Your documentation will look
like the .NET documentation, etc.
"keithv" <kve...@gmail.com> wrote in message
news:1105722604....@c13g2000cwb.googlegroups.com...
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"Marina" <som...@nospam.com> wrote in message
news:%23vHcQ6l%23EHA...@TK2MSFTNGP14.phx.gbl...
As thus:
private int empID;
public int EmpID { get set etc.}
you can put whatever validation code you want in the get/set accessors.
J
"keithv" <kve...@gmail.com> wrote in message
news:1105722604....@c13g2000cwb.googlegroups.com...
private double empID;
public int empID {
get { return (int)empID; }
set { empID = value; }
}
without breaking clients. I can also add validation in
private int empID;
public int empID {
get { return empID; }
set
{
if( empID == 0 )
throw new Exception(" emdID cannot be zero);
empID = value;
}
}
Regards
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
private int empID;
public int EmpID
{
get
{
return empID;
}
set
{
// validation code;
empID = value;
}
}
Or, to make empID ReadOnly, just omit the set accessor.
J
"John Adcock" <jad...@cox.net> wrote in message
news:OZAGQ%23l%23EHA...@TK2MSFTNGP09.phx.gbl...
-Rachel
"Daniel Jin" <Dani...@discussions.microsoft.com> wrote in message
news:433F0835-5383-4F49...@microsoft.com...
You can use the System.Diagnostics.DebuggerStepThroughAttribute to prevent
that. For trivial gets and sets I usually do this:
public IUIShell UIShell
{
[System.Diagnostics.DebuggerStepThrough]
get{ return this.uiShell; }
}
This is handled by the debugger, though, so if you have a debugger that
doesn't respect the attribute, it doesn't help. But then again, it's not
like people are using a wide range of different debuggers...
--
Arild
AnkhSVN - Subverting Visual Studio .NET: http://ankhsvn.tigris.org
Blog: http://ankhsvn.com/blog
IRC: irc://irc.freenode.net/ankhsvn
"Gentlemen, you can't fight in here! This is the war room!"
The problem arises when you want to step into a function
that takes a property as a parameter, e.g. foo(joe.EmpID);
You can't step into foo() w/o also stepping into the get.
Keith
Can you explain further? Yes there'll have to be a recompile
but that happens daily and I wouldn't consider that a "very
serious side effect".
Keith
Thanks! That's exactly the kind of arcana that I wanted to know.
Keith
1) Recompilation alone won't catch everything - you can pass a variable
by ref, but not a property, for example.
2) Recompilation may happen daily during development, but if anyone
else is developing components to work with your type, they may have
released a binary already...
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too