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
Recall & Review
beginner
What is a field in C#?
A field is a variable declared directly in a class or struct to store data. It holds the actual data for an object.
Click to reveal answer
beginner
What is a property in C#?
A property is a member that provides controlled access to a field, usually with get and set accessors to read or write the value.
Click to reveal answer
intermediate
Why use properties instead of fields?
Properties allow validation, encapsulation, and control over how data is accessed or changed, unlike fields which are accessed directly.
Click to reveal answer
beginner
Show a simple example of a property with a private field in C#.
private int age; public int Age { get { return age; } set { if (value >= 0) age = value; } }
Click to reveal answer
intermediate
What is an auto-implemented property?
An auto-implemented property lets you declare a property without explicitly defining a backing field; the compiler creates it automatically.
Click to reveal answer
Which of the following is true about fields in C#?
AFields store data directly and are usually private.
BFields provide controlled access with get and set methods.
CFields are only used in interfaces.
DFields cannot hold values.
✗ Incorrect
Fields are variables inside classes or structs that hold data directly, often marked private to protect data.
What does a property in C# typically include?
AGet and set accessors to control access to data.
BOnly a variable declaration.
CA method that returns void.
DA constructor.
✗ Incorrect
Properties usually have get and set accessors to read and write values with control.
Why might you prefer a property over a public field?
AProperties cannot be accessed outside the class.
BProperties are slower and less secure.
CFields can only be used in static classes.
DProperties allow adding validation logic when setting values.
✗ Incorrect
Properties let you add checks or other logic when values are read or changed, improving safety.
What is an auto-implemented property?
AA property that cannot be changed after creation.
BA property with no get or set accessors.
CA property where the compiler creates the backing field automatically.
DA property that is only readable.
✗ Incorrect
Auto-implemented properties let you write less code by not declaring the backing field explicitly.
Which keyword is used to define a property in C#?
Afield
Bget/set
Cproperty
Dclass
✗ Incorrect
Properties use get and set keywords to define accessors.
Explain the difference between a field and a property in C#.
Think about how you store data and how you control reading or writing it.
You got /4 concepts.
Describe why properties are preferred over public fields in object-oriented programming.
Consider how you keep data safe and flexible.
You got /4 concepts.
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
Step 1: Understand what a field does
A field is a variable inside a class that holds data directly.
Step 2: Understand what a property does
A property provides controlled access to data, often using get and set methods.
Final Answer:
Fields store data directly, properties control access to data. -> Option C
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
Step 1: Identify private field declaration
The private field should be lowercase (e.g., age) and declared as private int age;.
Step 2: Identify property syntax
The property Age uses get and set to access the private field correctly.
Final Answer:
private int age; public int Age { get { return age; } set { age = value; } } -> Option D
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
Step 1: Understand property set operation
The line p.Name = "Bob"; calls the set accessor, changing the private field name to "Bob".
Step 2: Understand property get operation
The line Console.WriteLine(p.Name); calls the get accessor, returning the updated value "Bob".
Final Answer:
Bob -> Option A
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
Step 1: Check field and property names
The field is speed and property is Speed, so no naming conflict.
Step 2: Analyze direct field access
Accessing c.speed directly bypasses the property, which can break encapsulation and safety.
Final Answer:
Accessing field directly breaks encapsulation. -> Option A
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
Step 1: Understand access needs
Balance should be readable publicly but only settable privately inside the class.
Step 2: Choose property with private set
Using public decimal Balance { get; private set; } allows public reading but restricts setting to inside the class.
Final Answer:
private decimal balance; public decimal Balance { get; private set; } -> Option B
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.