Top-level statements, introduced in C# 9.0, are a C# feature that lets you write executable program code directly in a source file (e.g. Program file) without explicitly creating a class and Main() method.
Definition: Top-level statements are executable statements written directly at the source-file level (outside any namespace or type declaration), which the compiler places into an automatically generated program entry method.
Because of Top-level statements features, instead of writing:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
You can write:
Console.WriteLine("Hello, World!");
The compiler automatically generates the equivalent Program class and Main() method.
Conceptually, the compiler treats it like:
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Characteristics of Top-level statements
- Only one file in your application can use top-level statements.
- The top-level statements cannot be enclosed in a namespace.
- When using top-level statements, the program cannot have a declared entry point i.e. Main() method. Other methods are allows except Main.
- Top-level statements still access a string array of args.
- The methods defined in the top-level statements file are local methods.
- Any types declared before the end of the top-level statements will result in a compilation error.
- Blocks are allowed to group executable statements in the top-level statements file.
Example. Type defined before top-level statements is invalid.
class Employee
{
// ERROR. Top-level statements must precede namespace and type declarations.
}
Console.WriteLine("Start");
Example. Type defined after top-level statements is valid.
Console.WriteLine("Start");
class Employee
{
}
Example. Type defined before the end of top-level statement will throw error.
Console.WriteLine("Start");
class Employee
{
// ERROR. Top-level statements must precede namespace and type declarations.
}
Console.WriteLine("End");
Example. Blocks are allowed in top level statement file.
//It is Valid to enclose top level statements inside block.
{
int x = 10; // local variable
Console.WriteLine($"First block variable x: {x}");
Console.WriteLine("Valid to enclose top level statements inside block.");
}
{
int x = 12; // local variable
Console.WriteLine($"Second block variable x: {x}");
Console.WriteLine("This is another block of top level statements.");
}
Example. Read command line arguments.
string[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine($"Argument 1: {arguments[0]}");
Console.WriteLine($"Argument 2: {arguments[1]}");
Console.WriteLine($"Argument 3: {arguments[0]}");
Note. The zero index argument returns the assembly path. On running the application in command line mode using command: dotnet run ajeet 22 we get the following output:
PS C:\Users\ajeet\Documents\appx\tempf\Practicals> dotnet run ajeet 22
Argument 1: C:\Users\ajeet\Documents\appx\tempf\Practicals\bin\Debug\net8.0\Practicals.dll
Argument 2: ajeet
Argument 3: 22
Question: Why Error Message: "Top-level statements must precede namespace and type declarations" for following code in Program file? What is solution?
public record Person(string FirstName, string LastName);
Person person1 = new Person("Ajeet","Kumar");
Answer:
This error occurs because in C#, top-level statements must appear before any type declarations (class, struct, record, interface, enum, etc.).
The code:
public record Person(string FirstName, string LastName);
Person person1 = new Person("Ajeet","Kumar");
has a type declaration (record Person) first, then a top-level statement (Person person1 = ...), which is not allowed.
Solution is:
Person person1 = new Person("Ajeet", "Kumar");
Console.WriteLine(person1);
// type declaration must be after top-level statements
public record Person(string FirstName, string LastName);
No comments:
Post a Comment