Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RequiredPropertiesInCSharp;

public class Customer
{
public required string Name { get; set; }
public string? Email { get; set; }
}
Original file line number Diff line number Diff line change
@@ -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()

Check warning on line 11 in csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Order.cs

View workflow job for this annotation

GitHub Actions / Build & test (csharp-intermediate-topics/RequiredPropertiesInCSharp)

Non-nullable property 'Customer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 11 in csharp-intermediate-topics/RequiredPropertiesInCSharp/RequiredPropertiesInCSharp/Order.cs

View workflow job for this annotation

GitHub Actions / Build & test (csharp-intermediate-topics/RequiredPropertiesInCSharp)

Non-nullable property 'Customer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace RequiredPropertiesInCSharp;

public record Product
{
public required string Sku { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace RequiredPropertiesInCSharp;
using System.Text.Json;

namespace RequiredPropertiesInCSharp;

public static class Program
{
Expand All @@ -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
Expand All @@ -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<Customer>("""{"Email":"ana@example.com"}""");
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message);
}
}

public static void DeserializeCustomerWithNullName()
{
var customer = JsonSerializer.Deserialize<Customer>("""{"Name":null}""");
Console.WriteLine($"Name is null: {customer!.Name is null}");

var options = new JsonSerializerOptions { RespectNullableAnnotations = true };
try
{
JsonSerializer.Deserialize<Customer>("""{"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<ClassWithRequiredProperty>();

public static T Create<T>() where T : new() => new T();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Loading