Csharp 12

De Banane Atomic
(Redirigé depuis Primary constructors)
Aller à la navigationAller à la recherche

Links

Primary constructors

Default value parameters must be compile time consistant.
Cs.svg
public class Item
{
    public readonly string Name { get; }

    public Item (string name)
    {
        Name = name;
    }
}

// equivalent with a primary constructor
public class Item(string name)
{
    public string Name { get; } = name;
}

Collection Expressions

Cs.svg
// initialization
int[] a     = [1, 2, 3];
List<int> l = [4, 5, 6];

// concat items from different collections with the spread element: ..e
int[] all = [.. a, .. l]; // [1, 2, 3, 4, 5, 6];

ref readonly modifier