1

While trying to create a simple list within C# I am getting the following error: 'The feature 'top-level statements' is currently in Preview and unsupported. To use Preview features, use the 'preview' language version. [5.1]'

At this point the only code I have entered is:

using System;
using System.Collections.Generic;

List<string> stringList = new List<string>();

Why would I be directed towards using a Preview version of C#, surely lists have been in use for a while?

3
  • 3
    You're trying to write code outside a class. Commented Aug 30, 2020 at 12:51
  • there´s no such thing as a "global" concept in .NET. Everything relates to classes, which build the fundament of OOP. So while lists are supported in .NET from the very beginning, variables outside a class are something that is under development. Commented Aug 30, 2020 at 12:58
  • The above scenario (Top Level Programs) is supported in C# 9 only, which is currently a preview feature. It is scheduled to be released in the coming November. Commented Aug 30, 2020 at 15:01

2 Answers 2

2

You can't just initialize a variable at the top level, it needs to be enclosed in a class if it's a member E.g.:

using System;
using System.Collections.Generic;

public class Program
{
    List<string> stringList = new List<string>();
}

Or inside a method if it's a local variable

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<string> stringList = new List<string>();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The error you recieve is not about lists, but about scopes. So the answer to

surely lists have been in use for a while?

is a definitive "yes". Lists (or at least arrays) exists since the very beginnings of C#, while generics followed in C# 2.0 which was supported from VS2005 onwards (for more information about the C# language-history see https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-version-history).

Your problem however is that you have a statement outside a class. However classes are the very fundamental concept behind OOP - everything relates to classes. The following - which has nothing to do with lists - would also be invalid:

using System;
using System.Collections.Generic;

int i = 3;

As mentioned before everything in .NET relates to classes - there´s no such conceptional thing as "global". So you have to move your statement into a class:

using System;
using System.Collections.Generic;

class MyClass
{
    List<string> stringList = new List<string>();
}

This will scope your list to a specific instance of MyClass. You can further limit the scope of a variable to a method or even a specific scope within a method. For further information about scopes read this.

After all the error-message you recieve relates to a new feature that is under discussion for C#9:

Allow a sequence of statements to occur right before the namespace_member_declarations of a compilation_unit (i.e. source file).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.