Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Properties vs fields in C Sharp (C#) - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Properties vs Fields Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing property vs field

What is the output of this C# code?

C Sharp (C#)
class Person {
    public string Name = "Alice";
    private int age = 30;
    public int Age {
        get { return age; }
        set { age = value; }
    }
}

var p = new Person();
Console.WriteLine(p.Name);
Console.WriteLine(p.Age);
Anull\n30
BAlice\n0
Cnull\n0
DAlice\n30
Attempts:
2 left
💡 Hint

Fields hold data directly. Properties can control access but here just return the field.

Predict Output
intermediate
2:00remaining
Effect of changing property setter

What is the output of this C# code?

C Sharp (C#)
class Counter {
    private int count = 0;
    public int Count {
        get { return count; }
        set { if (value > count) count = value; }
    }
}

var c = new Counter();
c.Count = 5;
c.Count = 3;
Console.WriteLine(c.Count);
A3
B5
C0
D8
Attempts:
2 left
💡 Hint

The setter only updates if the new value is greater than current.

🔧 Debug
advanced
2:00remaining
Why does this property cause a stack overflow?

What error does this code cause and why?

C Sharp (C#)
class Sample {
    private int value;
    public int Value {
        get { return value; }
        set { this.value = value; }
    }
}

var s = new Sample();
s.Value = 10;
Console.WriteLine(s.Value);
AStackOverflowException because property calls itself recursively
BNullReferenceException because value is null
CCompilation error due to missing semicolon
DOutput is 10
Attempts:
2 left
💡 Hint

The property getter and setter use the property name inside themselves.

🧠 Conceptual
advanced
2:00remaining
Difference between auto-property and field

Which statement correctly describes the difference between an auto-property and a field in C#?

AAn auto-property has hidden backing field and can have logic in get/set; a field is just data storage.
BA field can have get/set logic; an auto-property cannot.
CAn auto-property is slower because it uses reflection; a field is faster.
DA field can only be private; an auto-property can only be public.
Attempts:
2 left
💡 Hint

Think about what code the compiler generates for auto-properties.

Predict Output
expert
2:00remaining
Output of property with expression-bodied members

What is the output of this C# code?

C Sharp (C#)
class Rectangle {
    public int Width { get; set; }
    public int Height { get; set; }
    public int Area => Width * Height;
}

var r = new Rectangle { Width = 4, Height = 5 };
Console.WriteLine(r.Area);
ACompilation error
B9
C20
D0
Attempts:
2 left
💡 Hint

Expression-bodied properties calculate value on access.

Practice

(1/5)
1. What is the main difference between a field and a property in C#?
easy
A. Properties store data directly, fields control access to data.
B. Fields and properties are exactly the same in C#.
C. Fields store data directly, properties control access to data.
D. Properties can only be used in structs, fields only in classes.

Solution

  1. Step 1: Understand what a field does

    A field is a variable inside a class that holds data directly.
  2. Step 2: Understand what a property does

    A property provides controlled access to data, often using get and set methods.
  3. Final Answer:

    Fields store data directly, properties control access to data. -> Option C
  4. Quick Check:

    Field = direct data, Property = controlled access [OK]
Hint: Fields hold data; properties manage access to it. [OK]
Common Mistakes:
  • Confusing fields and properties as the same.
  • Thinking properties store data directly.
  • Believing fields control access like properties.
2. Which of the following is the correct syntax to declare a property named Age with a private field in C#?
easy
A. int Age; int Age { get; set; }
B. private int Age; public int Age { get; set; }
C. public int age; public int Age;
D. private int age; public int Age { get { return age; } set { age = value; } }

Solution

  1. Step 1: Identify private field declaration

    The private field should be lowercase (e.g., age) and declared as private int age;.
  2. Step 2: Identify property syntax

    The property Age uses get and set to access the private field correctly.
  3. Final Answer:

    private int age; public int Age { get { return age; } set { age = value; } } -> Option D
  4. Quick Check:

    Private field + property with get/set = correct syntax [OK]
Hint: Private field lowercase, property uppercase with get/set. [OK]
Common Mistakes:
  • Using same name for field and property causing errors.
  • Missing get or set in property.
  • Declaring fields as public when they should be private.
3. What will be the output of this C# code?
class Person {
  private string name = "Alice";
  public string Name {
    get { return name; }
    set { name = value; }
  }
}

var p = new Person();
p.Name = "Bob";
Console.WriteLine(p.Name);
medium
A. Bob
B. name
C. Alice
D. Compilation error

Solution

  1. Step 1: Understand property set operation

    The line p.Name = "Bob"; calls the set accessor, changing the private field name to "Bob".
  2. Step 2: Understand property get operation

    The line Console.WriteLine(p.Name); calls the get accessor, returning the updated value "Bob".
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Property set changes value, get returns updated value [OK]
Hint: Property set changes field; get returns updated value. [OK]
Common Mistakes:
  • Assuming output is original field value.
  • Confusing field name with property name.
  • Thinking code causes compilation error.
4. Identify the error in this code snippet:
class Car {
  public int speed;
  public int Speed {
    get { return speed; }
    set { speed = value; }
  }
}

var c = new Car();
c.Speed = 50;
Console.WriteLine(c.speed);
medium
A. Accessing field directly breaks encapsulation.
B. Field and property have the same name causing conflict.
C. No error; code works fine.
D. Property must be static to access field.

Solution

  1. Step 1: Check field and property names

    The field is speed and property is Speed, so no naming conflict.
  2. Step 2: Analyze direct field access

    Accessing c.speed directly bypasses the property, which can break encapsulation and safety.
  3. Final Answer:

    Accessing field directly breaks encapsulation. -> Option A
  4. Quick Check:

    Use properties to protect data, not direct field access [OK]
Hint: Avoid direct field access; use properties for safety. [OK]
Common Mistakes:
  • Thinking same names cause error (case-sensitive).
  • Believing direct field access is always safe.
  • Assuming property must be static.
5. You want to create a class BankAccount where the Balance can be read publicly but only changed inside the class. Which is the best way to declare Balance?
hard
A. public decimal Balance; // public field
B. private decimal balance; public decimal Balance { get; private set; }
C. public decimal Balance { private get; set; }
D. private decimal Balance; public decimal balance { get; set; }

Solution

  1. Step 1: Understand access needs

    Balance should be readable publicly but only settable privately inside the class.
  2. Step 2: Choose property with private set

    Using public decimal Balance { get; private set; } allows public reading but restricts setting to inside the class.
  3. Final Answer:

    private decimal balance; public decimal Balance { get; private set; } -> Option B
  4. Quick Check:

    Public get + private set = controlled access [OK]
Hint: Use property with private set for controlled write access. [OK]
Common Mistakes:
  • Using public field exposes data to unwanted changes.
  • Setting private get hides data from outside.
  • Confusing field and property naming conventions.