 Welcome to Attributes of a Class, Fields and Properties. In C-Sharp, attributes provide a way of associating data with an object and they take two forms, fields and properties. Fields store the data and properties allow access to the data. In this activity, we'll look at the differences between the two and how they should be used when creating code. An attribute describes an object. For example, a person's attributes include first name, last name, height, eye color, date of birth, and age. Each one of these attributes describes something specific about a person. Each instance of a class also has attributes. Let's look at a person class using some of the attributes we've listed. Public class person, private string first name, private string last name, private float height, which will represent height in inches, private string eye color, private date time, date of birth. As you can see, we've created private variables called fields for some of our attributes. By keeping these private, we've provided a bit of security for our objects and the only way to change or even get to the value of these variables is from within the person class. Now you might be asking, what's the use of having a class where none of the attributes are visible to the rest of the program? That's a good question. The answer is, typically it isn't very useful. So we need a way to access our fields. The way we do this in C sharp is by creating properties to access and set our variables. Using our previous example, let's create a property that allows us to get a person's name. The syntax is public string first name, get return name, set name equals value. More on that later. Let's break this down line by line. Even though the first line looks a lot like our name field, there are a few minor differences. First, we're using the public accessor instead of making it private. Next, notice we capitalize the word first name. We can't use a lowercase first name because that's already being used for the field. C sharp is a case sensitive language. So you'll often see field variables starting with a lowercase letter like name or with an underscore as an underscore name and their corresponding property starting with an uppercase letter. Finally, one more small difference is the addition of curly braces in place of semicolons. Inside the curly braces, we're required to add either a get statement, a set statement, or both statements. Let's look at the following code. Here we have a person class with some default values. Notice it has two properties, a first name property with only a get statement and a last name property with only a set statement. Public class person will initialize some of these fields to have default data. We'll set first name equal to Hussein. Last name equal to Ahmed. Height, 71. Eye color, brown. Date of birth, a new date time object, 1985, 130. We'll expose public properties, public string first name with a get statement, also known as a getter, returning first name, public string last name with a set statement, also known as a setter, last name equal to value. We'll talk about what value means shortly. In this next section, we'll create an instance of the person class. In this case, we're using a non-standard naming convention based on the Hungarian naming standard. We'll call our person Ohussein, where O represents object. While this is not a standard C-sharp naming convention, it's important to remember to use the naming convention provided by your instructor or where you were. Within our main method, we'll instantiate a new person object called Ohussein. Since the person class has a getter property for the first name, we can get the first name from Ohussein and store it in another variable. However, since there is no setter property, the next line generates an error when we try to assign a value to Ohussein dot first name. In contrast, since the person class doesn't have a getter property for the last name, an error is thrown when we try to assign Ohussein dot last name to a new variable. And unlike the first name, setting a new value to Ohussein dot last name won't generate an error because we've created a setter within the property. Because class fields are typically private, they're not accessible from code outside of the class. As we discovered earlier, access to those fields can be exposed and controlled via properties. In most cases, it makes sense to create get and set statements for your properties, such as public string last name get return last name, which returns the value stored in the last name field. Set last name equals value. Let's take a second look at the code inside of the get and set. Get is pretty straightforward. In this case, we're returning the value of the last name variable. However, set uses a keyword you may not have seen before. Value is a temporary variable used to pass in a value to the property. We actually used it in the previous example when we set Ohussein dot last name to equal Ahmed. So far, all of our examples have tied fields directly to properties, which often happens. Although properties can also be derived or computed from one or more fields. Let's look at two more examples demonstrating this. Public class person, we set the private variables to have some initial values. Let's take a look at our public properties. Public string full name get return first name plus space plus last name. Public int age get return date time dot now minus date of birth dot total days divided by 365 cast to int. Notice we have two properties, full name and age, and there are no fields called full name or age. In the case of the full name property, we're returning the first and last name fields concatenated with a space between them. Imagine for a second that we have fields storing first name, last name, and full name. Not only is this data redundant, but what happens when Ohussein changes his name? Now we have to change the data in multiple places. By creating a property for the full name, the redundant data is removed and the complexity of the code is reduced. The same is true for the age property. If we have someone's date of birth, we can calculate how old they are. To review, attributes of a class take two forms in C-sharp, fields and properties. Fields store the data and in most cases should be kept private. Private string first name. Properties on the other hand allow access to the data and are usually public. They also have a get statement, a set statement, or both. You have completed attributes of a class, fields and properties.