In a class, an instance field is a kind of class member that is a variable declared in the class and having a particular type. There is one copy of the field for each instance of the class. You can read and write the value of the field using a reference to the instance of the class.
Fields are declared in a class as follows:
1
2
3
4
5
| public class Dog{ public string Name; public int Age;} |
These fields can then be used as follows:
1
2
3
4
5
6
7
8
9
10
| Dog buster = new Dog(); // Create new instance of Dogbuster.Name = "Buster"; // Write to Name fieldbuster.Age = 3; // Write to Age fieldDog kirby = new Dog(); // Another instance of a Dogkirby.Name = "Kirby";kirby.Age = 13;// Reading propertiesint agesAdded = buster.Age + kirby.Age; |

