Challenge - 5 Problems
Auto-Implemented Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of auto-implemented property usage
What is the output of this C# code snippet using auto-implemented properties?
C Sharp (C#)
public class Person { public string Name { get; set; } } var p = new Person(); p.Name = "Alice"; System.Console.WriteLine(p.Name);
Attempts:
2 left
💡 Hint
Auto-implemented properties automatically create a hidden field to store the value.
✗ Incorrect
The property Name is auto-implemented with get and set. Assigning "Alice" to p.Name stores it, so printing p.Name outputs "Alice".
🧠 Conceptual
intermediate1:30remaining
Default value of auto-implemented property
What is the default value of an auto-implemented property of type int in C# if not explicitly set?
Attempts:
2 left
💡 Hint
Consider the default value of value types in C#.
✗ Incorrect
Auto-implemented properties use backing fields. For int, the default value is 0 if not set.
🔧 Debug
advanced2:30remaining
Identify the error in auto-implemented property declaration
What error does this code produce?
public class Car {
public string Model { get; }
}
var c = new Car();
c.Model = "Tesla";
C Sharp (C#)
public class Car { public string Model { get; } } var c = new Car(); c.Model = "Tesla";
Attempts:
2 left
💡 Hint
Check if the property has a setter.
✗ Incorrect
The property Model has only a getter, so assigning to it causes a compile-time error CS0200.
📝 Syntax
advanced2:00remaining
Correct syntax for auto-implemented property with private setter
Which option correctly declares an auto-implemented property named Age with a public getter and a private setter?
Attempts:
2 left
💡 Hint
The access modifier for the setter goes before 'set;' inside the property.
✗ Incorrect
Option D uses the correct syntax for a private setter with a public getter.
🚀 Application
expert3:00remaining
Number of items in dictionary from auto-implemented property usage
Consider this C# code:
public class Item {
public int Id { get; set; }
}
var items = new List- {
new Item { Id = 1 },
new Item { Id = 2 },
new Item { Id = 1 }
};
var dict = items.ToDictionary(i => i.Id);
How many items does dict contain after execution?
C Sharp (C#)
public class Item { public int Id { get; set; } } var items = new List<Item> { new Item { Id = 1 }, new Item { Id = 2 }, new Item { Id = 1 } }; var dict = items.ToDictionary(i => i.Id); System.Console.WriteLine(dict.Count);
Attempts:
2 left
💡 Hint
Check if keys in a dictionary must be unique.
✗ Incorrect
The ToDictionary method throws an exception because there are duplicate keys (Id=1) in the list.