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?

Re: attributes vs. properties 80 80

Attributes in c# terms are different, while you and I call a persons eyes an attributes, in computring terms they are properties.

Re: attributes vs. properties 80 80

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...

Re: attributes vs. properties 80 80

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";
    }
Re: attributes vs. properties 80 80

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.

Re: attributes vs. properties 80 80

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; }
}

Re: attributes vs. properties 80 80

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.

Re: attributes vs. properties 80 80

Thanks for helping me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.