0
0
C Sharp (C#)programming~5 mins

Top-level statements in modern C# - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are top-level statements in modern C#?
Top-level statements allow you to write C# code directly without needing to define a class or Main method explicitly. The compiler creates the entry point automatically.
Click to reveal answer
beginner
How do top-level statements simplify writing a C# program?
They let you write code directly at the file's root, removing boilerplate like class and Main method declarations, making small programs easier and faster to write.
Click to reveal answer
intermediate
Can you use namespaces and classes with top-level statements?
Yes, you can still use namespaces and classes in the same file with top-level statements. The top-level code runs before any other code in classes or namespaces.
Click to reveal answer
intermediate
What happens if you declare a Main method in a file with top-level statements?
If you declare a Main method explicitly, the compiler will produce an error because top-level statements already define the program's entry point.
Click to reveal answer
beginner
Which version of C# introduced top-level statements?
Top-level statements were introduced in C# 9.0, released with .NET 5 in 2020.
Click to reveal answer
What is the main benefit of using top-level statements in C#?
AYou must write more code to define entry points.
BYou can write code without defining a class or Main method.
CThey remove the need for namespaces.
DThey allow multiple Main methods in one file.
Which C# version introduced top-level statements?
AC# 9.0
BC# 8.0
CC# 7.0
DC# 10.0
Can you mix top-level statements with classes in the same file?
ANo, top-level statements must be alone.
BOnly if you remove namespaces.
COnly if classes are declared after top-level statements.
DYes, classes and namespaces can coexist with top-level statements.
What happens if you declare a Main method in a file with top-level statements?
AThe compiler produces an error due to multiple entry points.
BThe program runs both Main and top-level code.
CThe Main method is ignored.
DThe top-level statements are ignored.
Which of the following is true about top-level statements?
AThey require explicit Main method declaration.
BThey remove the need for using directives.
CThey simplify small programs by reducing boilerplate.
DThey only work in console applications.
Explain what top-level statements are and how they change writing C# programs.
Think about how you start writing a simple program without extra code.
You got /3 concepts.
    Describe how top-level statements interact with namespaces and classes in the same file.
    Consider if you can still organize code in files with top-level statements.
    You got /3 concepts.