Object initializers are an alternative to constructors. The syntax is illustrated below. You provide a comma separated list of name-value pairs separated with =
within curly brackets:
public class Person
{
public string Name;
public string Address;
}
var person = new Person{Name="The President", Address = "Élysée Palace"};
Collections can also be initialized in this way. Typically, this is accomplished with comma separated lists as shown here:
IList<Person> people = new List<Person>{ new Person(), new Person{Name="Joe Shmo"}};
Dictionaries use the following syntax:
IDictionary<int, string> numbers = new Dictionary<int, string>{ [0] = "zero", [1] = "one"...};
// or
IDictionary<int, string> numbers = new Dictionary<int, string>{ {0, "zero" }, {1, "one"}...};
You've been asked to do some more work on the network authentication system.
In addition to the admin identity being hard-coded in the system during development the powers that be also want senior developers to be given the same treatment.
The admin's details are as follows:
Eye Color | Philtrum Width | Name | Address 1 | Address 2 | |
---|---|---|---|---|---|
admin@ex.ism | green | 0.9 | Chanakya | Mumbai | India |
Implement the Authenticator.Admin
property to return the system admin's identity details. The name and each part of the address should be in a separate element of the NameAndAddress
list.
var authenticator = new Authenticator();
authenticator.Admin;
// => {"admin@ex.ism", {"green", 0.9m}, ["Chanakya", "Mumbai", "India"]}
The developers' details are as follows:
Eye Color | Philtrum Width | Name | Address 1 | Address 2 | |
---|---|---|---|---|---|
bert@ex.ism | blue | 0.8 | Bertrand | Paris | France |
anders@ex.ism | brown | 0.85 | Anders | Redmond | USA |
Implement the Authenticator.Developers
property to return the developers' identity details. The dictionary key is the developer's name.
var authenticator = new Authenticator();
authenticator.Developers;
// => ["Bertrand"] = {"bert@ex.ism", {"blue", 0.8m}, ["Bertrand", "Paris", "France"]},
// ["Anders"] = {"anders@ex.ism", {"brown", 0.85m}, ["Anders", "Redmond", "USA"]},
Sign up to Exercism to learn and master C# with 62 concepts, 167 exercises, and real human mentoring, all for free.