attributes vs. properties

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 4
Reputation: legend_018 is an unknown quantity at this point 
Solved Threads: 0
legend_018 legend_018 is offline Offline
Newbie Poster

attributes vs. properties

 
0
  #1
Oct 4th, 2008
Hi. I'm trying to learn c# using some online tutorials. At one point, it was teaching me that objects are defined by attributes (data) and behaviors. Example of an Object could be a person with eye, color, and height as attributes.

Than later on, I'm learning about properties. The examples give height and width.

So than i'm thinking, hmmm height is giving as an example as an attribute when trying to explain objects and than height is also giving as an example when trying to show code examples for properties.

Than I start getting confused thinking so are attributes the same as properties? Why am I getting confused here?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: attributes vs. properties

 
0
  #2
Oct 5th, 2008
Attributes in c# terms are different, while you and I call a persons eyes an attributes, in computring terms they are properties.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: okutbay is an unknown quantity at this point 
Solved Threads: 0
okutbay okutbay is offline Offline
Newbie Poster

Re: attributes vs. properties

 
0
  #3
Oct 5th, 2008
in oop entity attributes maps to class data fields...

if you have a person class you can have a data field to hold a height value of a person...

Data fields are private in general... and properties are special methods to give access to these private class members...

Sample:

public class Person
{
private int height; // this is a data field
public int Height // this is a property
{
get { return this.height; } //getter method of the property. returns the backing data field's value
set { this.height = value; } //setter method of the property. sets the backing data field's value
}
}

Properties can have only a get or set... The we call properties read only or write only properties according the methods they have...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: okutbay is an unknown quantity at this point 
Solved Threads: 0
okutbay okutbay is offline Offline
Newbie Poster

Re: attributes vs. properties

 
0
  #4
Oct 5th, 2008
In C# you can think attributes are special compiler directives... Don't mix them with entity attributes...

For example you can mark a method obsolete with obsolete attribute in c#

[Obsolete("Test method is obsolete... Please update your codes.",true)] //an attribute for method
public string TestMethod()
{
return "Test Method";
}
Last edited by okutbay; Oct 5th, 2008 at 1:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: attributes vs. properties

 
0
  #5
Oct 5th, 2008
An attribute as okutbay says are like

[Flags]

a property is

class thing
{
public String test { get; set; }
}

If you think of it that way its easy not to get confused.

classes etc may have attributes, but they arent properties such as eye color, finger count, height, width etc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: okutbay is an unknown quantity at this point 
Solved Threads: 0
okutbay okutbay is offline Offline
Newbie Poster

Re: attributes vs. properties

 
0
  #6
Oct 5th, 2008
a note about LizR's sample...

public String test { get; set; } syntax new in c# 3.5... In this syntax backing private data field and property methods generated by compiler automatically

Generated code looks like this..

private String _test;
public String test
{
get { return this._test; }
set { this._test = value; }
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: attributes vs. properties

 
0
  #7
Oct 5th, 2008
Yep, its just a way to save your self time if you dont need to do anything special in the get/set of a property.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: legend_018 is an unknown quantity at this point 
Solved Threads: 0
legend_018 legend_018 is offline Offline
Newbie Poster

Re: attributes vs. properties

 
0
  #8
Oct 5th, 2008
Thanks for helping me.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC