« Csharp 12 » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp = Links = * [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12 What's new in C# 12] ») |
|||
(8 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 2 : | Ligne 2 : | ||
= Links = | = Links = | ||
* [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12 What's new in C# 12] | * [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12 What's new in C# 12] | ||
= [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors Primary constructors] = | |||
{{warn | Default value parameters must be compile time consistant.}} | |||
<kode lang='cs'> | |||
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; | |||
} | |||
</kode> | |||
= [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions Collection Expressions] = | |||
<kode lang='cs'> | |||
// 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]; | |||
</kode> | |||
= [[Parameter_modifiers#ref_readonly|ref readonly modifier]] = |
Dernière version du 16 septembre 2024 à 13:33
Links
Primary constructors
Default value parameters must be compile time consistant. |
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
// 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]; |