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

Auto-implemented properties in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Auto-Implemented Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AName
Bnull
CAlice
DCompilation error
Attempts:
2 left
💡 Hint
Auto-implemented properties automatically create a hidden field to store the value.
🧠 Conceptual
intermediate
1: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?
Anull
B0
C1
DCompilation error
Attempts:
2 left
💡 Hint
Consider the default value of value types in C#.
🔧 Debug
advanced
2: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";
ACS0200: Property or indexer 'Car.Model' cannot be assigned to -- it is read only
BCS0120: An object reference is required for the non-static field, method, or property
CNo error, outputs Tesla
DCS0501: 'Car.Model.set' must declare a body because it is not marked abstract or extern
Attempts:
2 left
💡 Hint
Check if the property has a setter.
📝 Syntax
advanced
2: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?
Apublic int Age { get private; set; }
Bpublic int Age { private get; set; }
Cpublic int Age { get; set; private }
Dpublic int Age { get; private set; }
Attempts:
2 left
💡 Hint
The access modifier for the setter goes before 'set;' inside the property.
🚀 Application
expert
3: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);
ARuntime exception
B3
C1
D2
Attempts:
2 left
💡 Hint
Check if keys in a dictionary must be unique.