Questions tagged [c#-10.0]

C# 10 introduced features like file scoped namespace declaration, global usings, lambda improvements, record structs and string interpolation improvements. Use this tag if your question specifically pertains to C# 10 specific features. The C# tag should be used if your question is not specific to C# 10 features.

178 questions
236
votes
14 answers

Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable

I have a simple class like this. public class Greeting { public string From { get; set; } public string To { get; set; } public string Message { get; set; } } Strangely I get the following warning. Severity Code Description…
VivekDev
  • 20,868
  • 27
  • 132
  • 202
85
votes
4 answers

VS 2022 - Convert to file-scoped namespace in all files

I'm converting my project to .NET 6 and I want to use filescoped namespaces everywhere. But the conversion tool exists only in the editor. Has anyone found out if there's a way to run this editor function across all files in solution at once?…
Mirek
  • 4,013
  • 2
  • 32
  • 47
59
votes
3 answers

C# 10: Disable Global Using

How can I disable the new default global usings in C# 10 (see here: https://github.com/dotnet/aspnetcore/issues/32451)? I want to see the used namespaces at a glance and don't want to look up the documentation which namespaces are used globally.
Martinaut
  • 1,868
  • 1
  • 17
  • 27
54
votes
3 answers

DateTime.Now equivalent for TimeOnly and DateOnly?

.NET 6 / C# 10 introduced TimeOnly and DateOnly structs, to represent only a time and only a date respectively. The good old DateTime struct always had a Now static property which would give you the current date and time. I was expecting both…
Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
31
votes
2 answers

EditorConfig control File-scoped namespace declaration

I'm using C# 10 new feature File-scoped namespace declaration. I have old code like this namespace SampleCode { public class MyClass { } } I'm moving this code to namespace SampleCode; public class MyClass { } But I have a bunch of…
JuChom
  • 5,717
  • 5
  • 45
  • 78
20
votes
2 answers

ASP.NET Minimal API - Access IConfiguration

Is it possible to access the the IConfiguration in the new ASP.NET Minimal API? I do not see the possibility to do such thing. using Microsoft.AspNetCore.Components; using MudBlazor.Services; var builder = WebApplication.CreateBuilder(args); //…
serious
  • 525
  • 10
  • 21
18
votes
2 answers

Init-only reference properties with nullable enabled in C# 10.0

I tried to use init-only properties to force client code to initialize my class when they create it, but without a constructor. It's not working as I planned. Here's the class, stripped down to illustrate the point. public class Target { public…
Joe
  • 5,394
  • 3
  • 23
  • 54
16
votes
1 answer

How to use C#10 in Visual Studio 2019

How can I use C# 10 in Visual Studio 2019? I have latest update (16.11.6) and when I try to make a new project (need NET Standard 2.0, for compatibility with 4.7.2) and change it to C#10
jahav
  • 699
  • 1
  • 7
  • 24
13
votes
3 answers

Where are the using statements/directives in .NET 6

I got up and running with Visual Studio 2022 Preview for a couple of days now. Got the first shock, there is no Startup.cs. Thats ok, a bit of reading, I know Startup is removed. Today got another slap. I see no using statements. Here it is. I just…
VivekDev
  • 20,868
  • 27
  • 132
  • 202
10
votes
1 answer

When to use record struct instead of struct, and vice versa?

I have found out recently about the record keyword in C#, and saw that it can be used as record struct in a way to make it, if I understood correctly, a value type instead of a reference type. However, I'm having a hard time to understand when…
10
votes
3 answers

ASP.NET Minimal API How to Return/Download Files from URL

I'm working on minimal api, what I'm trying is when the user visits /download it immediately downloads my picture named add.png. But no matter what I try it doesn't work because I either get an empty page with only {} Is this possible? if so…
Teun
  • 161
  • 1
  • 1
  • 8
10
votes
1 answer

C#10 nullable pattern: how to tell the compiler I set the non-nullable property in the constructor indirectly?

Consider an example: class Test { string S { get; set; } public Test() { Init(); } private void Init() { S = "hello"; } } Using nullable C# project feature, this sample will trigger a compiler…
Harry
  • 4,524
  • 4
  • 42
  • 81
9
votes
4 answers

C#10 unclear CS8600 warning

I'm currently migrating a Blazor project to .NET6 which comes with C# 10. With the new language version I'm getting a warning with the following code: if (tmp is null) oldValue = ""; else oldValue = tmp.ToString(); The warning is: CS8600…
UnusualWays
  • 177
  • 1
  • 2
  • 11
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)); } …
8
votes
1 answer

C# 9/10 top-level statements and ExcludeFromCodeCoverage-Attribute?

I usually set the attribute [ExcludeFromCodeCoverage] to my Program class, as there are no unit tests for this class possible anyways (or don't make sense either), so it doesn't show up as "missing" in the coverage…
DominikAmon
  • 892
  • 1
  • 14
  • 26
1
2 3
11 12