| | |
attributes vs. properties
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
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";
}
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.
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
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; }
}
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; }
}
![]() |
Similar Threads
- Checkbox has no properties in FireFox. Works fine in IE (JavaScript / DHTML / AJAX)
- Please help me!!! (Java)
- has no properties... (JavaScript / DHTML / AJAX)
- Real Estate Properties Databse (Database Design)
- Composition (Java)
- Style properties are ignored by Mozilla browser... (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: to be install once
- Next Thread: Patching problem
| Thread Tools | Search this Thread |
.net access ado.net algorithm array asp.net barchart bitmap box broadcast button buttons c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf xml






