Skip to Content
Course content

5: Top-Level Statements

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably noticed by now that C# has a habit of wrapping everything in a lot of "ceremony." To write even a single line of logic, you usually need a namespace, a class, and a static Main method. It's structurally sound, but when you're just trying to prototype a quick tool or write a simple script, it feels like a lot of noise.

That's why Top-Level Statements were introduced. They essentially let you tell the compiler: "I know what I'm doing, just assume this whole file is the Main method." It makes your code look much more like Python or JavaScript, which is a breath of fresh air when you're just trying to get something running.

Cutting through the noise with a Quick-Task Logger

Let's build a tiny CLI tool that lets you log a task and timestamps it. In the old days, I'd have to set up a Program class. Now, I can just start typing logic directly into Program.cs. I'm going to start by capturing some user input and printing a formatted string.

Console.WriteLine("What are you working on right now?");
string task = Console.ReadLine();
string timestamp = DateTime.Now.ToString("HH:mm");

Console.WriteLine($"[{timestamp}] Logged: {task}");

That's it. No class, no curly braces, no static void Main(string[] args). The compiler handles all that behind the scenes. I personally love this for "glue code" or small utilities where the overhead of a full class structure just gets in the way of the actual logic.

The "Method Placement" snag

Now, as our tool grows, I want to move the timestamp formatting into its own method to keep the main flow clean. I'll try to define a helper method at the top of the file, because that's where I usually like to put my utilities.

string GetTimestamp() 
{
    return DateTime.Now.ToString("HH:mm");
}

Console.WriteLine("What are you working on right now?");
string task = Console.ReadLine();
Console.WriteLine($"[{GetTimestamp()}] Logged: {task}");

If you try to run this, the compiler will yell at you. You'll get an error saying that top-level statements must precede any method declarations. I've made this mistake more times than I'd like to admit. In a standard class, you can put methods anywhere. But with top-level statements, the "main" logic has to come first. Anything defined as a method must live at the bottom of the file.

Ordering the logic correctly

To fix this, I just need to shift my helper method to the end. Think of the file as having two zones: the "Execution Zone" at the top and the "Definition Zone" at the bottom.

Console.WriteLine("What are you working on right now?");
string task = Console.ReadLine();

// The execution happens here
Console.WriteLine($"[{GetTimestamp()}] Logged: {task}");

// Now we define our helpers at the bottom
string GetTimestamp() 
{
    return DateTime.Now.ToString("HH:mm");
}

Now it compiles perfectly. One last thing to keep in mind: you can only have one file with top-level statements in your entire project. If you try to do this in two different files, the compiler won't know which one is the actual entry point of your application, and it'll throw a fit. For everything else in your project, you'll still use the standard class and method structure we've discussed in previous lessons.




📋 Practical Task

Build a Top-Level "Budget Calculator"

Create a new C# console application using top-level statements. Your program should:

  • Prompt the user to enter their total monthly budget (as a double).
  • Prompt the user to enter a specific expense amount.
  • Call a helper method named CalculateRemaining that takes the budget and the expense as parameters and returns the remaining balance.
  • Print the final remaining balance to the console.

Constraint: Ensure your CalculateRemaining method is placed correctly so the code compiles without "top-level statement" ordering errors.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.