C#C
C#6mo ago
BiznessCat

Top Level Statements Issue

Heyo, I was following along with a friend making a small console app, but ran into some issues cause the version i was using had top level statements while they were coding on was pre 6.0


This was where I stopped at first
// See https://aka.ms/new-console-template for more information using System.Security.AccessControl; using Bank_App; private static List<Account> accounts = new List<Account>(); bool isAppRunning = true; while (isAppRunning) { DisplayMenu(); int choice = GetChoice(); switch (choice) { case 1: AddAccount(); break; case 2: case 3: case 4: case 5: Console.WriteLine("Exiting App"); isAppRunning = false; break; default: Console.WriteLine("Invalid Choice"); break; } } static void DisplayMenu() { Console.Clear(); Console.WriteLine("MENU"); Console.WriteLine("-------"); Console.WriteLine("1. Add Account "); Console.WriteLine("2. View All Accounts"); Console.WriteLine("3. Deposit "); Console.WriteLine("4. Withdraw "); Console.WriteLine("5. Exit "); } static int GetChoice() { Console.WriteLine("Choose an option: "); int choice; while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 5) { Console.WriteLine("Please enter a valid choice."); } return choice; } static void AddAccount() { Console.Clear(); Console.WriteLine("Enter Account Holder Name: "); string name = Console.ReadLine(); int accountNumber = accounts.Count + 1; Account newAccount = new Account(accountNumber, name); accounts.Add(newAccount); Console.WriteLine($"A new account has been created for {name} with the account number {accountNumber}"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
APP.cs(6, 30): [CS0116] A namespace cannot directly contain members such as fields, methods or statements
Was the error I got.

Any chance someone can explain what exactly went wrong. I still dont fully understand how top level statements work and would like to deal with it because most tutorials were made before it was introduced

This was what I eventually got after switching to TLS
Was this page helpful?