943,782 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4478
  • C# RSS
Oct 4th, 2008
0

attributes vs. properties

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
legend_018 is offline Offline
4 posts
since Oct 2008
Oct 5th, 2008
0

Re: attributes vs. properties

Attributes in c# terms are different, while you and I call a persons eyes an attributes, in computring terms they are properties.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 5th, 2008
0

Re: attributes vs. properties

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
okutbay is offline Offline
6 posts
since Oct 2008
Oct 5th, 2008
0

Re: attributes vs. properties

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
okutbay is offline Offline
6 posts
since Oct 2008
Oct 5th, 2008
0

Re: attributes vs. properties

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 5th, 2008
0

Re: attributes vs. properties

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; }
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
okutbay is offline Offline
6 posts
since Oct 2008
Oct 5th, 2008
0

Re: attributes vs. properties

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 5th, 2008
0

Re: attributes vs. properties

Thanks for helping me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
legend_018 is offline Offline
4 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: to be install once
Next Thread in C# Forum Timeline: Patching problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC