public class Cat { // Auto-implemented properties. public int Age { get; set; } public string Name { get; set; } public Cat() { } public Cat(string name) { this.Name = name; } }
var moreNumbers = new Dictionary<int, string>
{
{19, "nineteen" },
{23, "twenty-three" },
{42, "forty-two" }
};
Starting with C# 6, object initializers can set indexers, in addition to assigning fields and properties. Consider this basic Matrix
class:
public class Matrix {
private double[,] storage = new double[3, 3];
public double this[int row, int column] { // The embedded array will throw out of range exceptions as appropriate. get { return storage[row, column]; } set { storage[row, column] = value; } } }
var identity = new Matrix { [
public static void Main()
{
FormattedAddresses addresses = new FormattedAddresses()
{
{"John", "Doe", "123 Street", "Topeka", "KS", "00000" },
{"Jane", "Smith", "456 Street", "Topeka", "KS", "00000" }
};
Console.WriteLine("Address Entries:");
foreach (string addressEntry in addresses)
{
Console.WriteLine("
" + addressEntry);
}
}
/*
* Prints:
Address Entries:
John Doe
123 Street
Topeka, KS 00000
Jane Smith
456 Street
Topeka, KS 00000
*/
}