Recall & Review
beginner
What is a static member in C#?
A static member belongs to the class itself, not to any specific object. It can be accessed without creating an instance of the class.Click to reveal answer
beginner
What is an instance member in C#?
An instance member belongs to a specific object created from a class. You need to create an object to access instance members.
Click to reveal answer
intermediate
Can static members access instance members directly? Why or why not?
No, static members cannot access instance members directly because instance members belong to objects, and static members belong to the class itself without any object context.Click to reveal answer
beginner
How do you call a static method and an instance method in C#?
You call a static method using the class name (e.g., ClassName.Method()), and you call an instance method using an object of the class (e.g., objectName.Method()).Click to reveal answer
beginner
Example: What will this code print?
<pre>class Car {
public static int wheels = 4;
public string color;
public Car(string c) { color = c; }
}
Console.WriteLine(Car.wheels);
Car myCar = new Car("red");
Console.WriteLine(myCar.color);</pre>It will print:
4
red
Because 'wheels' is static and accessed by class name, and 'color' is instance and accessed by object.Click to reveal answer
Which of these belongs to the class itself, not to any object?
✗ Incorrect
Static members belong to the class itself and can be accessed without creating an object.
How do you access an instance member in C#?
✗ Incorrect
Instance members require an object to be accessed because they belong to that specific object.
Can a static method access instance variables directly?
✗ Incorrect
Static methods do not have access to instance variables because they do not belong to any object.
What keyword is used to declare a static member in C#?
✗ Incorrect
The 'static' keyword declares a member that belongs to the class itself.
If you want a property shared by all objects of a class, which should you use?
✗ Incorrect
Static members are shared across all instances of the class.
Explain the difference between static members and instance members in your own words.
Think about whether you need to create an object to use the member.
You got /4 concepts.
Describe a real-life example where you would use a static member versus an instance member.
Imagine a car factory: number of wheels is same for all cars, color is different per car.
You got /4 concepts.