1

So I have VS 2022 and am trying to learn C#.

I am getting an error of Feature

'file-scoped namespace' is not available in C# 7.3 Please use language 10.0 or greater.

I found where one goes to Options>Code Style> Text Editor and change to File-scoped. Did that.

When I create my solution, I chose Blank Solution I then added an empty project using .NET framework 4.8 Next I added a code file Finally I add the following:

namespace Averages;

public static class TestClass
{
 
}

And I get the error. What in the world am I missing? I'm stumped. Edit: I just checked. I have .NET 7.0

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
WinginSue
  • 60
  • 6
  • 2
    You're using the old .NET Framework, which only supports C# language version 7.3. If you want to use file-scoped namespaces, you need to use a version of .NET that supports C# language version 10. This means you need to use at least .NET version 6.0. [See here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version). –  Mar 26 '23 at 22:41
  • I don't think this is a typo, for someone who is learning, confusing syntax from C# versions could be a problem, not a keyboard accident – Leandro Bardelli Mar 26 '23 at 22:44
  • 2
    If you are learning I strongly suggest using .NET 6.0 or later, not .NET Framework 4.8 – Corvus Mar 26 '23 at 22:58

2 Answers2

5

The reason you can't use this syntax is because you created your project using .NET Framework 4.8. This is the old version of .NET, which only supports C# language version 7.3.

See the image below -- the top one is for the newest .NET versions (5.0+), the bottom one is for the old .NET Framework (4.x and below). This applies to class libraries too. enter image description here

File-scoped namespaces are a C#10 language feature, which are only available in .NET >= 6.0. That is why you can't use it in your .NET Framework 4.8 project.

You would need to create a project that supports the newer .NET versions, *not .NET Framework*, if you want to use the latest C# language features.

This Microsoft article describes the relationship between C#/.NET versions.

1

You could solve this by:

namespace Averages 
{ 
   public static class TestClass
   {
 
   }
}

since Framework 4.8 doesn't allow File-scoped names, so the class should be "inside" the namespace.

But if you are learning, I would suggest don't use empty blank, syntax could be a bit different, but in learning mode this bit means a lot and you will found examples of code that often doesn't specify the syntax.

You could also try your code at Fiddle DOT NET https://dotnetfiddle.net/

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 1
    Thanks. I am able to fix it with the solution you have in the code. I was just trying to figure out why I can't get it to work. I had conundrums. LOl – WinginSue Mar 26 '23 at 22:45
  • 1
    I will wait a bit to see if someone has another idea. I did upvote it though. It does help. I just can't figure out why it doesn't work. I know I can use Fiddle DOT Net or Visual Studio Code or a multitude of other products it's just weird that this doesn't work for me. – WinginSue Mar 26 '23 at 22:54
  • 1
    I tried creating a Console App instead of an empty one. Now it works. I have NO idea what's different. That did it . Thanks Leandro. I clicked yours as the answer. – WinginSue Mar 26 '23 at 23:01
  • @WinginSue thanks! My suggestion for the fiddle is that, meanwhile some code I use TDD to develop, I still use dot net fiddle to test some things that Im trying to learn. – Leandro Bardelli Mar 26 '23 at 23:01
  • @WinginSue for console apps you can found two things. First one and more evident, the selection of the framework (NET Framework or NET 6/7). From NET 6 or above, also you can check/uncheck "Do not use top-level statments". Since I'm old school, I check this one because I like everything, the namespace, the class, the whole thing. If do leave it, no namespace or class will appear and the code will "simplify" (not for me, at least) – Leandro Bardelli Mar 26 '23 at 23:05