0
0
C Sharp (C#)programming~5 mins

Instance fields and state in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
AData unique to each object
BData shared by all objects
COnly constant values
DTemporary method variables
How do you declare an instance field in a class?
AInside a method
BUsing the static keyword
COutside methods but inside the class
DOnly in interfaces
Which keyword makes a field shared by all objects of a class?
Ashared
Bstatic
Cinstance
Dpublic
If you create two objects of a class, how many copies of an instance field exist?
ATwo copies, one per object
BNone
COne copy shared by both
DDepends on the field type
How do you access an instance field named 'age' of an object 'person'?
Aage.person()
Bage.person
Cperson->age
Dperson.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.