Run cs file without main method using NET 10

Run cs file without main method using .NET 10


You don't have to explicitly include a main method in a console application project. Instead, you can use the top-level statements feature to minimize the code you have to write.

Top-level statements allow you to write executable code directly at the root of a file, eliminating the need for wrapping your code in a class or method. This means you can create programs without the ceremony of a Programm class and a Mainmethod. In this case, the compiler generates a Program class with an entry point method for the application. The name of the generated method isn't Main it's an implementation detail that your code can't reference directly.


1. create ".cs" file, lets create "app.cs"

2. Open folder path in any terminal

3. Run the file using command "dotnet run aap.cs"

Note : Prerequisite .NET 10 

Example statements

1. Console.WriteLine("Hello World!");

 output : Hello World!

2. 
int a =10;
int b=15;
int c= a+b;

Console.WriteLine($"Result : {c}");

output : Result : 25



Comments

Popular posts from this blog

Dev tunnels

Creating an executable installer (.exe) in Visual Studio

Create installable exe file from windows form application in visual studio