C# - CS8803 Top-level statements must precede namespace and type declarations

Earlier I was putting some code together for an example. I got the following error (Top-level statements must precede namespace and type declarations):

Error	CS8803	Top-level statements must precede namespace and type declarations.	ConsoleApp2	C:\Users\peter\source\repos\ConsoleApp2\ConsoleApp2\Program.cs	25

The code looked like the following:

public static class StringExtensions
{
    public static string ToCommaSeperatedString(this IEnumerable<string> list)
    {
        return string.Join(",", list);
    }
}

var someList = new List<string>
{
   "A","B","C"
};

Console.WriteLine(someList.ToCommaSeperatedString());

In the above we declare a new type of class (StringExtensions) and then we create a list of strings, however with Top-level statements (TLS) you must declare your namespaces and types - for example classes - first. There are two solution to this, the code can be rearranged so that the class is defined last:

var someList = new List<string>
{
   "A","B","C"
};

Console.WriteLine(someList.ToCommaSeperatedString());

public static class StringExtentions
{
    public static string ToCommaSeperatedString(this IEnumerable<string> list)
    {
        return string.Join(",", list);
    }
}

Alternatively we can go back to the old way of making entry points with a static void Main method:

class Program
{
    static void Main(string[] args)
    {
        var someList = new List<string>
        {
            "A","B","C"
        };

        Console.WriteLine(someList.ToCommaSeperatedString());
    }
}

public static class StringExtentions
{
    public static string ToCommaSeperatedString(this IEnumerable<string> list)
    {
        return string.Join(",", list);
    }
}

I hope you found this helpful, please leave a comment down below if you did!