Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Object-Oriented Programming Fields

Glenn Dietz
Glenn Dietz
613 Points

Suggest: attribute - field discussion

After a rough start through this course yesterday I decided to start over. This pass I listened and took notes for understanding instead of trying to keep up with the coding.

I was surprised yesterday when I got to the challenge for this lesson and the word "field" was used. I had missed the switch from "attribute" to "field" that occurs quickly when Jeremy switched from discussion to coding. I would suggest that a sentence or two of that change would help.

Thanks, Glenn

Good question, I too would like to have a better understanding, any more experienced people have an answer of the terminology 'field'? Is this equivalent to what is called a 'property' in JavaScript or an 'attribute' in Python, or is there something else to differentiate it?

1 Answer

Allan Clark
Allan Clark
10,810 Points

Good question! The difference here is mainly semantics. When he says 'attribute' at the beginning of the video he is using the normal dictionary version of the term 'attribute'. (i.e. 'a quality or feature regarded as a characteristic or inherent part of someone or something') What he means is conceptually height and width are a defining characteristic of the map therefore they will need to have their own fields (actually more likely they will be properties, more on that later). When he switches to the code he begins to use more programmer centered terminology.

When it comes to code Attributes are their own beasts that have nothing to do with fields (or properties). A good example of a C# Attribute is the [Authorize] tag. What this does is verify that a user is Authorized via OAuth before executing a controller action (mmmm MVC goodness). https://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx

[Authorize]          // <----- Attribute
public void Foo()
{
   //do stuff
}

So yes his terminology should be a little more concise to avoid confusion.

This will be a little ahead of the specific video you asked this on, but as this is months later you probably have gotten past that point by now. Properties essentially are the same as fields (this is a simplification of course). They hold the state for the class and are quicker to read and write, especially when it comes to controlling the access to the field. It is generally best practice to use properties rather than fields. (Pretty sure the last time I manually made a field in C# was when i ran through this course haha) But that comes later in the course.

Hope this helps!