معرفی واژهی کلیدی جدید required در C# 11
نویسنده: وحید نصیری
تاریخ: ۱۴۰۱/۰۸/۲۳ ۱۲:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Article1
{
public string Title { get; set; }
public string? Subtitle { get; set; }
public string Author { get; set; }
public DateTime Published { get; set; }
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project> Non-nullable property 'Title' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [CS11Tests]csharp(CS8618) Non-nullable property 'Author' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [CS11Tests]csharp(CS8618)
public class Article2
{
public Article2(string title, string subtitle, string author, DateTime published)
{
Title = title;
Subtitle = subtitle;
Author = author;
Published = published;
}
public Article2(string title, string author, DateTime published)
{
Title = title;
Author = author;
Published = published;
}
public string Title { get; set; }
public string? Subtitle { get; set; }
public string Author { get; set; }
public DateTime Published { get; set; }
} Article2 article = new("C# 11 Required Keyword", "A new language feature", "Name", new DateTime(2022, 11, 12)); public string Title { get; set; } = default!; public class Article3
{
public required string Title { get; set; }
public string? Subtitle { get; set; }
public required string Author { get; set; }
public DateTime Published { get; set; }
}
public class Book
{
public Book() => Name = string.Empty;
public Book(string name) => Name = name;
public required string Name { get; set; }
} Book book = new("Book's Name"); Required member 'Book.Name' must be set in the object initializer or attribute constructor. [CS11Tests]csharp(CS9035)
public class Book
{
[SetsRequiredMembers]
public Book() => Name = string.Empty;
[SetsRequiredMembers]
public Book(string name) => Name = name;
public required string Name { get; set; }
} public class OldCarDto
{
public string Brand { get; set; }
public string Model { get; set; }
public uint Horsepower { get; set; }
} var json = """
[
{
"brand": "Ferrari",
"horsePower": 651
},
{
"model": "F50",
"horsePower": 512
}
]
"""; var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var oldResults = JsonSerializer.Deserialize<List<OldCarDto>>(json, options); public class NewCarDto
{
public required string Brand { get; init; }
public required string Model { get; init; }
public required uint Horsepower { get; init; }
} var newResults = JsonSerializer.Deserialize<List<NewCarDto>>(json, options);
System.Text.Json.JsonException: JSON deserialization for type 'NewCarDto' was missing required properties, including the following: model