Question field vs attribute vs property

Rolex

New member
Joined
Nov 22, 2013
Messages
2
Programming Experience
1-3
Hello everyone,

I am rather new to .net. I've been struggling with the basics: C# terminology.
Can anyone help me to get what is the difference among these terms, i.e. 'field', 'property' and 'attribute'?
The reason for asking is that some sources use them as synonyms, others deliberately avoid using 'attribute' or 'property' etc.
Seems a mess. The MSDN glossary is not helpful either.

My present understanding is:
- field: the component of a class. Describes the class: what does the class look like, what are its characteristics etc.
- attribute: a special tag that marks a field. There most of the confusion comes: what sort of tags? Where they can be put? What do they mean?
- property: a special method that manages access to fields (which in turn should always be private); is marked by 'set' and 'get' key words.

Is it correct?

Thanks in advance
 
In general speech, those terms often are used interchangeably and that does carry over into some poorly written programming texts. Each one does have a specific meaning in C# programming but there are also times when you want to refer to something in a more general sense and end up having to use one of those words because there's nothing else appropriate.

A field is a member variable. A local variable is one declared inside a method and a variable declared outside any method is a member variable.

A property is logically data exposed by an object that behaves like a field on the outside but behaves like a method(s) on the inside. In Java, it is convention to use public accessor methods to access private fields rather than exposing the field publicly, e.g.
private string data;

public string get_Data()
{
    return data;
}

public void set_Data(string value)
{
    data = value;
}
C# has formalised that convention into properties, where the getter and setter are combined into a single entity, e.g.
private string data;

public string Data
{
    get { return data; }
    set { data = value; }
}
The main reason for not exposing fields directly is that it allows you to do things other than just set the field, e.g. validate the incoming value or raise an event when the stored value changes. If you don't need to do anything extra then C# simplifies the syntax further so that it's barely more code than a straight field, e.g.
public string Data { get; set; }
In the last two cases, the compiled code actually looks the same as the first, i.e. the compiler generates a backing field and two accessor methods regardless. The rest is just syntactic sugar.

An attribute is metadata, i.e. information about a type or member as opposed to information in a type or member. Attributes can be applied to types or members, depending on the attribute itself, and are used by code in applications and tools to decide how to use those types and members. For instance, if you create a custom control and apply the Browsable attribute to a property and set the value to False then that property will not show up in the Properties window when you select that control on a form in the designer.
 
Back
Top Bottom