From 40070a97868b2bfa45cb72c6fea7069e3a72e7ae Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sat, 14 Nov 2026 14:08:03 +0100 Subject: [PATCH] Update Required Members sample to .NET 11 and add record, Order and JSON examples --- .../RequiredPropertiesInCSharp/Customer.cs | 7 +++ .../RequiredPropertiesInCSharp/Order.cs | 14 +++++ .../RequiredPropertiesInCSharp/Product.cs | 6 ++ .../RequiredPropertiesInCSharp/Program.cs | 61 +++++++++++++++++-- .../RequiredPropertiesInCSharp.csproj | 2 +- 5 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Customer.cs create mode 100644 csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Order.cs create mode 100644 csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Product.cs diff --git a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Customer.cs b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Customer.cs new file mode 100644 index 0000000000..cc89d48778 --- /dev/null +++ b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Customer.cs @@ -0,0 +1,7 @@ +namespace RequiredPropertiesInCSharp; + +public class Customer +{ + public required string Name { get; set; } + public string? Email { get; set; } +} diff --git a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Order.cs b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Order.cs new file mode 100644 index 0000000000..d760563c47 --- /dev/null +++ b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Order.cs @@ -0,0 +1,14 @@ +using System.Diagnostics.CodeAnalysis; + +namespace RequiredPropertiesInCSharp; + +public class Order +{ + public required string Customer { get; set; } + public required int Quantity { get; set; } + + [SetsRequiredMembers] + public Order() + { + } +} diff --git a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Product.cs b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Product.cs new file mode 100644 index 0000000000..7d9d87736e --- /dev/null +++ b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Product.cs @@ -0,0 +1,6 @@ +namespace RequiredPropertiesInCSharp; + +public record Product +{ + public required string Sku { get; init; } +} diff --git a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Program.cs b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Program.cs index b6a227d9b8..8dd33f53a5 100644 --- a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Program.cs +++ b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Program.cs @@ -1,4 +1,6 @@ -namespace RequiredPropertiesInCSharp; +using System.Text.Json; + +namespace RequiredPropertiesInCSharp; public static class Program { @@ -7,19 +9,23 @@ public static void Main() InstantiateClassWithRequiredProperty(); InstantiateClassWithRequiredPropertyAndCustomConstructor(); InstantiateClassWithRequiredPropertyAndAnnotatedCustomConstructor(); + InstantiateRecordWithRequiredInitProperty(); + InstantiateOrderWithForgetfulConstructor(); + DeserializeCustomerWithMissingName(); + DeserializeCustomerWithNullName(); } public static void InstantiateClassWithRequiredProperty() { - // Uncommenting this line results in an error because required properties were not initialized. - // var classWithRequiredProperty = new RewrittenClass(); + // Uncommenting this line results in error CS9035 because the required property is not initialized. + // var classWithRequiredProperty = new ClassWithRequiredProperty(); var classWithRequiredProperty = new ClassWithRequiredProperty() { RequiredString = "Hello World" }; } public static void InstantiateClassWithRequiredPropertyAndCustomConstructor() { - // Uncommenting this line results in an error because required properties were not initialized. + // Uncommenting this line results in error CS9035 because the required property is not initialized. // var classWithRequiredPropertyAndCustomConstructor = new ClassWithRequiredPropertyAndCustomConstructor("Hello World"); var classWithRequiredPropertyAndCustomConstructor @@ -31,4 +37,51 @@ public static void InstantiateClassWithRequiredPropertyAndAnnotatedCustomConstru var classWithRequiredPropertyAndAnnotatedCustomConstructor = new ClassWithRequiredPropertyAndAnnotatedCustomConstructor("Hello World"); } + + public static void InstantiateRecordWithRequiredInitProperty() + { + var product = new Product { Sku = "A-1" }; + + Console.WriteLine(product); + } + + public static void InstantiateOrderWithForgetfulConstructor() + { + var order = new Order(); + + Console.WriteLine($"Customer is null: {order.Customer is null}, Quantity: {order.Quantity}"); + } + + public static void DeserializeCustomerWithMissingName() + { + try + { + JsonSerializer.Deserialize("""{"Email":"ana@example.com"}"""); + } + catch (JsonException ex) + { + Console.WriteLine(ex.Message); + } + } + + public static void DeserializeCustomerWithNullName() + { + var customer = JsonSerializer.Deserialize("""{"Name":null}"""); + Console.WriteLine($"Name is null: {customer!.Name is null}"); + + var options = new JsonSerializerOptions { RespectNullableAnnotations = true }; + try + { + JsonSerializer.Deserialize("""{"Name":null}""", options); + } + catch (JsonException ex) + { + Console.WriteLine(ex.Message); + } + } + + // Uncommenting this method results in error CS9040 because ClassWithRequiredProperty has required members. + // public static ClassWithRequiredProperty CreateWithNewConstraint() => Create(); + + public static T Create() where T : new() => new T(); } diff --git a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp.csproj b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp.csproj index d439800007..7de2e17cda 100644 --- a/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp.csproj +++ b/csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net11.0 enable enable