Questions tagged [c#-11.0]

C# 11 will introduce features like parameter null checking, list patterns, static abstracts in interfaces and the field keyword . Use this tag if your question specifically pertains to C# 11 specific features. The C# tag should be used if your question is not specific to C# 11 features.

68 questions
82
votes
3 answers

What is double exclamation mark in C#?

From https://source.dot.net/#System.Private.CoreLib/Hashtable.cs,475: public virtual bool ContainsKey(object key!!) It looks like two null-forgiving operators. Is there a document about it?
shingo
  • 18,436
  • 5
  • 23
  • 42
16
votes
1 answer

What is the purpose of the 'scoped' keyword

While looking at the source code of the new DefaultInterpolatedStringHandler I noticed that the ReadOnlySpan was annotated with the scoped keyword. The only documentation I could find was here. However, I wasn't able to figure what the practical…
Twenty
  • 5,234
  • 4
  • 32
  • 67
9
votes
3 answers

EF Core model classes: Use C# 11 `required` modifier on all properties, even on value type properties?

When using the nullable-enable feature in Entity Framework Core model classes, the compiler will emit a lot of CS8618 warning like that: warning CS8618: Non-nullable property 'Title' must contain a non-null value when exiting constructor. Consider…
eikuh
  • 673
  • 1
  • 9
  • 18
8
votes
1 answer

CA1062 is thrown after updating to !! parameter null checking

According to CA1062 null checks are required in externally visible methods like this: public static double GetLongitude(this Location location) { if(location is null) { throw new ArgumentNullException(nameof(location)); } …
6
votes
2 answers

C# 11 escape rules for ref parameters: ref int vs Span

Why does the following code not compile in C# 11? // Example 1 - fails class C { public Span M(ref int arg) { Span span; span = new Span(ref arg); return span; } } It produces two compile…
Bartosz
  • 461
  • 2
  • 9
5
votes
2 answers

Write other C# code in an interpolated string?

In C# 11 we can now include newlines in an interpolated string. So we can write code like this: string pageTitle = ""; string header = $"Header: { pageTitle switch { "" => "No title", _ => pageTitle }}"; Is…
DeborahK
  • 57,520
  • 12
  • 104
  • 129
5
votes
1 answer

Is it possible to auto-implement IEquatable and IComparable in C# 11?

When writing C#, I tend to write a lot of value type objects that implement IEquatable, IComparable, or both. For the sake of this proposal, let's assume that I'm writing a fictitious struct called Int256 with equatable and comparable value…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
5
votes
1 answer

Generic math how to cast from INumber to a "concrete" type (int/double/etc)

Suppose we have a generic method which accepts INumber and returns int: // process val and return as int static int DoSomething(T val) where T : INumber => ... How can the T be casted to int.
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
5
votes
1 answer

C# 11 - What have we gained by adding IAdditionOperators to this method?

I just upgraded Visual Studio 2022 to .NET7, which includes C# 11. I am interested in trying out the new static abstract interface methods, and so followed the tutorial there. The article shows how to define Point and Translation records that use…
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
5
votes
2 answers

Why am I getting warnings that analysers are not found?

I created a toy project for checking out the latest .NET 7 (preview-5) and the regex code generation. It worked great, so I applied the same changes to an existing project (not for production, but for personal productivity). For some reason, I’m…
Quirin F. Schroll
  • 1,302
  • 1
  • 11
  • 25
4
votes
1 answer

Required init property with backing field gives null warning C# 11

Having a required init property which sets a backing field still gives a null warning. The below code gives a warning: Warning CS8618 Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider declaring the field…
Joas
  • 1,796
  • 1
  • 12
  • 25
4
votes
1 answer

C# 11 - detect required property by reflection

C# 11 added support for required properties. public class Example { public required string Value { get; set; } } How do I detect that the property is declared as required by reflection? Please note this is a different question from Return a…
Mirek
  • 4,013
  • 2
  • 32
  • 47
3
votes
1 answer

Using Numeric literals/constants in Generic Math C# 11

I am working with the new Generic Math System in C# 11, which is cool, but I haven't found a way to use numeric literals or mathematical constants with them. The following example which calculates the circumference of a circle does not work. public…
3
votes
1 answer

Alternative to the new C# 11 `required` modifier in C# 10 and earlier

.NET 7 and C# 11 introduce a new modifier required. Documentation says: The required modifier indicates that the field or property it's applied to must be initialized by all constructors or by using an object initializer. Any expression that…
bahrep
  • 29,961
  • 12
  • 103
  • 150
3
votes
3 answers

Can I set SetsRequiredMembers or another attribute for only one member in C# 11?

Consider the following code: public record Foo { public required string A { get; init; } public required string B { get; init; } } public record Bar : Foo { public Bar() { A = "TEST"; } } var bar = new Bar { B = "ANOTHER…
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
1
2 3 4 5