Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can anyone help me with classes that are not working c#(.NET 7.0)

I am creating a basic console application(I am very new to c#) and I am creating a game class to allow for a multi-file project.But It is spitting out an error:

Top-level statements must precede namespace and type declarations. 

the class itself is not getting any errors but the code after the class IS.
here is an example to illustrate my point:

public class game 
{
    Console.WriteLine();
}

int u = 0;

Bare in mind that I do not know how namespaces work in c# and all the examples I could find are in .NET 6.0 or lower

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance:D

>Solution :

namespace you_namespace
{
public class game 
 {
    public void output(int u)
     {
     Console.WriteLine($"u = {u}");
     }
 }
}

Classes live in namespaces, so I created one to contain the game
class. Variable declarations and initialisations need to be inside a class or a method like output(). Just fooling around here, but it’s the principle you have to grasp. I advice to take a beginners course in C#, to start off prepared.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading