473,699 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is a property?

Hi there,

can someone tell me what exactly a "property" is in a C# class? As far as I
can see it is "two methods" - ie a getter and a setter for an instance
variable.

What is the difference between these:

public string InstanceStringV ariable()
{
get { return _instanceString Variable; }
set { _instanceString Variable = value; }
}

and

public string getInstanceStri ngVariable
{
return _instanceString Variable;
}

public void setInstanceStri ngVariable(stri ng stringVariable)
{
_instanceString Variable = stringVariable;
}

Thanks,
Peter
Nov 17 '05
13 1809
KH
I tend to think of properties as adjectives and methods as verbs - properties
describe the object - color, name, size - whereas methods are actions -
Read(), Write(), Dispose(), Jump(), etc.

It should probably be noted that properties get turned into methods by the
compiler. So for example if you had a property:

object MyProperty
{
get { throw new Exception(); }
}

and you called .MyProperty, it would be referred to in the call stack as:

get_MyProperty( object value)

"Jon Shemitz" wrote:
Peter Kirk wrote:
can someone tell me what exactly a "property" is in a C# class? As far as I
can see it is "two methods" - ie a getter and a setter for an instance
variable.


Well, "one or two methods" - you have can have a get without a set (or
a set without a get, though the semantics are pretty weird, there).
What is the difference between these:

public string InstanceStringV ariable()
{
get { return _instanceString Variable; }
set { _instanceString Variable = value; }
}

and

public string getInstanceStri ngVariable
{
return _instanceString Variable;
}

public void setInstanceStri ngVariable(stri ng stringVariable)
{
_instanceString Variable = stringVariable;
}


First, as a number of people have pointed out, a property is a lot
easier to read.

if (This.Bool)

vs

if (this.getBool() )

and the like.

Second, property methods are linked. Declare a property abstract, or
virtual, or static, and both methods are abstract, or virtual, or
static. It would be hard (impossible?) to enforce this linkage with a
get/set convention.

Third, properties ease maintenance / allow optimization. A member
might be a field, because setting it has no side-effects, and you want
to allow fast read/write. As your requirements change, you can easily
change the field to a property without rewriting all the client code.

--

www.midnightbeach.com

Nov 17 '05 #11
Peter:

There are a million different ways to skin a cat. The fact that you have
notion of a getter and a setter (as we all do) is an indication that it
might as well be added as a language feature and fully supported.

I think also that properties are good for thinking in terms of objects. When
I envision systems, my first level of thought is objects consisting of
events, methods, and properties (EMP). Objects of this nature form
relationships with other objects to perform work and provide services.
Properties allow an object to present a public face to the system.
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:uy******** ******@TK2MSFTN GP14.phx.gbl...
Hi there,

can someone tell me what exactly a "property" is in a C# class? As far as
I can see it is "two methods" - ie a getter and a setter for an instance
variable.

What is the difference between these:

public string InstanceStringV ariable()
{
get { return _instanceString Variable; }
set { _instanceString Variable = value; }
}

and

public string getInstanceStri ngVariable
{
return _instanceString Variable;
}

public void setInstanceStri ngVariable(stri ng stringVariable)
{
_instanceString Variable = stringVariable;
}

Thanks,
Peter

Nov 17 '05 #12
In message <uH************ **@TK2MSFTNGP10 .phx.gbl>, Christof Nordiek
<cn@nospam.de > writes
Hi Peter,
It is also possible via getter and setter methods. What exact benefit does
a "property" give us over getter and setter methods?


Simply look at this Example and decide yourselve, wich is more readable:

oldText = control.get_Tex t();
control.get_Te xt(newText);

vs.

oldText = control.Text;
constrol.Tex t = newText;


Or

position.X++;

versus

position.set_X( position.get_X( )+1);

--
Steve Walker
Nov 17 '05 #13
"Peter Kirk" <pk@alpha-solutions.dk> skrev i en meddelelse
news:uy******** ******@TK2MSFTN GP14.phx.gbl...
can someone tell me what exactly a "property" is in a C# class? As far as
I can see it is "two methods" - ie a getter and a setter for an instance
variable.


Thanks for all the helpful and informative answers.

Peter
Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
11249
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What is the difference between a Field and a Property? Here is the context. In the VS.NET 2002 documentation, in the "String Members" section, we have the following...
9
1068
by: Mark Jones | last post by:
This rather cryptic title is actually a .net oop noob question. Example: My customer object has a public property called .name which is a string. I would like to be able to reference mycust.name.length which would return the string length of the value of name. I can't nest property/subs so how do I accomplish this? Another example would be adding something like .tostring to my own
11
1452
by: ucasesoftware | last post by:
If i have this property Dim m_name as string Property name() as string Get return m_name end Get Set (byval Value as string) m_name = Value
14
2378
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to hassel with a multi-dimensional array. Is there such an object in VB.Net? Dim obj As someCollectionObj obj.Add("parmA1", "parmA2", "parmA3") obj.Add("parmB1", "parmB2", "parmB3") ....
5
2383
by: Cylix | last post by:
this.menus = { root: new Array };
6
2863
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for example) or is doing some long calculations.
10
2104
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset statements into comments (placed an ' in front)
5
2237
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
6
4029
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new types of objects (classes) can only be defined in Class modules.
1
1986
by: developing | last post by:
Hello, This time I want to filter the 'browse for file' API so that certain drives/folders cant be accessed. Not sure how to go about this...here is the module I am currently using to browse for a file then return the file link... any ideas/help are highly appreciated. Option Explicit Option Compare Database
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.