Aller au contenu

« Csharp 11 » : différence entre les versions

De Banane Atomic
Aucun résumé des modifications
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
[[Category:CSharp]]
[[Category:CSharp]]
= Liens =
= Links =
* [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11 What's new in C# 11]
* [https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11 What's new in C# 11]
= [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties#required-properties Required members] =
{{info | Don't confuse {{boxx|required}} with non-nullable. It's valid to set a {{boxx|required}} property to {{boxx|null}} or {{boxx|default}}.}}
Any code that creates a new Item must set this property using an [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers object initializer].
<kode lang='cs'>
public class Item
{
    public required string Name { get; set; }
}
var item = new Item { Name = "item1" }; // object initializer for Name property is now required
</kode>


= [[String#Raw_string_literals|Raw string literals]] =
= [[String#Raw_string_literals|Raw string literals]] =

Dernière version du 18 janvier 2025 à 14:12

Links

Required members

Don't confuse required with non-nullable. It's valid to set a required property to null or default.

Any code that creates a new Item must set this property using an object initializer.

public class Item
{
    public required string Name { get; set; }
}

var item = new Item { Name = "item1" }; // object initializer for Name property is now required

Raw string literals