Recall & Review
beginner
What is an instance field in C#?
An instance field is a variable declared inside a class but outside any method. Each object (instance) of the class has its own copy of this variable, which holds the object's state.Click to reveal answer
beginner
How does an instance field differ from a static field?
An instance field belongs to each object separately, so each object has its own value. A static field belongs to the class itself and is shared by all objects of that class.Click to reveal answer
beginner
Why do instance fields represent the state of an object?
Because instance fields store data unique to each object, they describe the current condition or characteristics of that object, which is called its state.
Click to reveal answer
beginner
Consider this code snippet:<br><pre>class Car { public string color; }</pre><br>What does the 'color' field represent?The 'color' field is an instance field that stores the color of each Car object. Each Car can have a different color stored in its own 'color' field.
Click to reveal answer
beginner
How do you access an instance field in C#?
You access an instance field through an object of the class using the dot (.) operator. For example: <code>myObject.fieldName</code>.Click to reveal answer
What does an instance field store in C#?
✗ Incorrect
Instance fields hold data unique to each object, representing its state.
How do you declare an instance field in a class?
✗ Incorrect
Instance fields are declared inside the class but outside any method.
Which keyword makes a field shared by all objects of a class?
✗ Incorrect
The 'static' keyword makes a field shared by all instances of the class.
If you create two objects of a class, how many copies of an instance field exist?
✗ Incorrect
Each object has its own copy of instance fields.
How do you access an instance field named 'age' of an object 'person'?
✗ Incorrect
Use the dot operator: objectName.fieldName, so person.age.
Explain what instance fields are and how they relate to an object's state.
Think about how each object remembers its own information.
You got /4 concepts.
Describe the difference between instance fields and static fields in C#.
Consider who owns the data: the object or the class.
You got /4 concepts.