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

Static members vs instance members in C Sharp (C#) - Quick Revision & Key Differences

Choose your learning style9 modes available
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?
AParameter
BStatic member
CLocal variable
DInstance member
How do you access an instance member in C#?
AUsing the class name
BDirectly without any reference
CUsing an object of the class
DUsing the static keyword
Can a static method access instance variables directly?
AYes, always
BOnly inside constructors
COnly if the instance variables are public
DNo, because instance variables belong to objects
What keyword is used to declare a static member in C#?
Astatic
Binstance
Cconst
Dreadonly
If you want a property shared by all objects of a class, which should you use?
AStatic member
BInstance member
CLocal variable
DParameter
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.