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?

Recommended Answers

All 7 Replies

Attributes in c# terms are different, while you and I call a persons eyes an attributes, in computring terms they are 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...

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

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.

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

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.

Thanks for helping me.

Be a part of the DaniWeb community

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