Questions tagged [toplevel-statement]

C# 9 introduces top-level statements which let to write programs with less boilerplate code.

18 questions
12
votes
2 answers

How to define a method following top-level statements

I recently updated Visual Studio and found out about this new feature (to me it is new) of top-level statements. As I understand it, the compiler completes the definitions for the Program class and Main method, without you having to explicitly type…
McJason
  • 133
  • 1
  • 7
9
votes
1 answer

Is there a way to unit test top-level statements in C#?

I was fiddling with top-level statements as the entry point for a simple console app, since the new .NET 6 template use them as a default. Yet, as the language specification very clearly states: Note that the names "Program" and "Main" are used…
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
7
votes
4 answers

"Top-level statements must precede namespace and type declarations"

So I haven't been coding for long so I'm not so experienced, I recently ran into a problem on replit.com where the console would print out: error CS8803: Top-level statements must precede namespace and type declarations. using System; could anyone…
aidan
  • 79
  • 1
  • 1
  • 2
6
votes
1 answer

Under what conditions will the generated Top Level Statement class be $Program?

I've always been under the assumption that the class generated by Top Level Statements was a hidden, inaccessible class. For example: System.Console.WriteLine(2); partial class Program { public static string abc = "def"; } When ran in…
gunr2171
  • 16,104
  • 25
  • 61
  • 88
6
votes
2 answers

C# Only one compilation unit can have top-level statements

I just started learning c#, I created C# console application. To understand the concepts, I watched videos of how to setup vs code for c# When I run the dotnet new console command in VS code terminal, it creates a new project including Program.cs…
3
votes
2 answers

How to use Logger in program.cs in .NET 6, before builder.Build()

Using the default WebAPI framework VS2022 in .NET 6. I would like to log information using ILogger, but before the call of "var app = builder.Build();". Is it possible to retrieve the logger newly configured by "buider.Host.ConfigureLogging(logging…
3
votes
3 answers

How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements?

Assume I have a simple script writing in C# 9 like this: using System; using System.IO; // What to put in the ??? var exeFolder = Path.GetDirectoryName(typeof(???).Assembly.Location); Before, with the full program, we can use the Main class as an…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
2
votes
1 answer

How to access the top-level statement variable in a class in C#?

I have a simple code below. I want to access the variable x in the class Program. As x is a global variable, I should be able to access it, Is there a way to access the top-level variable apart from the top level? int x = 0; namespace…
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
2
votes
1 answer

How do I fix the top-level statement's error?

Program1.cs Regular C# file, works perfectly. Random numberGen = new Random(); int roll1 = 1; int roll2 = 0; int roll3 = 0; int roll4 = 0; int attempts = 0; Console.WriteLine("Press enter to roll the dies"); while (roll1 != roll2 || roll2 !=…
Canyon
  • 61
  • 1
  • 6
1
vote
1 answer

Missing partial modifier on declaration of type 'Program'

I started a project with the new console template that uses top level statements, but I wanted to add Program Main back in anyway. However, when I add in the class like this: // See https://aka.ms/new-console-template for more…
KyleMit
  • 30,350
  • 66
  • 462
  • 664
1
vote
0 answers

Rider IDE huge usage logo

Does anyone know if this is a bug or something? I've tried to google it but can't find a solution. And is it impossible to turn off top-level statements in c# rider?
aron kan
  • 72
  • 8
1
vote
2 answers

Will adding a namespace and a Main method to Program.cs in ASP.NET Core 6 have any negative effects?

I have an ASP.NET Core 6 Web Api application. Currently, the Program.cs uses the new program style with top-level statements as described here. This means that it does not have a namespace. There is a bug in SonarQube 9.3 which raises an error on…
MiBuena
  • 451
  • 3
  • 22
1
vote
1 answer

Where to define delegates in .Net 6 Console template using top level statements?

I recently tried the .Net 6 console template, with top level statements, in Visual studio and stumbled into a Gotcha. If you try to compile the below code, Visual Studio will give a red squiggly line under the string declaration var s = "myString";.…
FluffyBike
  • 825
  • 1
  • 4
  • 17
1
vote
1 answer

SA1200 in the new .NET 6 Program.cs

.NET 6 introduces a new bootstrap syntax that replaces the old Program.cs/Startup.cs mishmosh. The standard template looks like this: using ThetaRex.Common; var builder = WebApplication.CreateBuilder(args); // Add services to the…
Quark Soup
  • 4,272
  • 3
  • 42
  • 74
1
2