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

Properties vs fields in C Sharp (C#) - Quick Revision & Key Differences

Choose your learning style9 modes available
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.
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.
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.
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.
Which keyword is used to define a property in C#?
Afield
Bget/set
Cproperty
Dclass
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.