11

My understanding is that it is similar to write code directly into the old "static void Main(string[] args)" without the need to display what's above.

However, I used to declare my variables in the class Program to have them accessible from other classes (apologies if not best practice, I learnt C# by myself and as long as it works, I'm happy with my code). See example below:

using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;

namespace myNameSpace
{
    class Program
    {
        //variables declaration
        public static string abc = "abc";
        public static int xyz = 1;

        static void Main(string[] args)
        {
            //code goes here
        }
    }
}

With C# 9, it seems I can only declare variables in the Main section, so how can I declare them to be able to access them from other classes?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Guil75
  • 125
  • 1
  • 6

3 Answers3

19

I don't think the currently-accepted answer is correct, if you toss a partial class signature in Program.cs you can most certainly add things like static-scoped fields and attributes:

var customAttributes = (CustomAttribute[])typeof(Program).GetCustomAttributes(typeof(CustomAttribute), true);
Console.WriteLine(customAttributes[0].SomePropery);
Console.WriteLine(MyField);


[Custom(SomePropery = "hello world")]
public partial class Program
{ 
    private const string MyField = "value";
}

class CustomAttribute : Attribute
{
    public string SomePropery { get; set; }
}

The above code and nothing else in Program.cs will output:

/home/dongus/bazinga/bin/Debug/net6.0/bazinga
hello world
value

Process finished with exit code 0.

I use this method to apply the [ExcludeFromCodeCoverage] attribute to my projects' Program.cs files

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
dongus
  • 325
  • 1
  • 5
  • Agree, this should be the accepted answer, because it isn't a dismissive "just don't use it if you need to do something else" response (when that doesn't have to be the case). I can confirm I can put an attribute on the top-level `Program.cs` by just adding a partial class `Program` to the bottom of the file. (You can't put it at the top, as you'll get compiler errors.) I had needed to add `[ExcludeFromCodeCoverage]`, and this way worked like a charm! – Doctor Blue Jul 29 '22 at 12:17
  • I'm doing a lot of research, I wasn't aware of this option, and I'd [really like to understand why it works](https://stackoverflow.com/q/73167975/1043380). – gunr2171 Jul 29 '22 at 15:55
  • "I don't think the currently-accepted answer is correct" - it was as of original .NET 5 feature implementation. Though with introduction of minimal hosting model with .NET 6 the generational patterns were changed to support [integration testing in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0#aspnet-core-6-9) I assume. – Guru Stron Apr 10 '23 at 06:32
12

For .NET 5:

When you use the Top-Level Program feature of C# 9, you give up the ability to put anything outside the Main method scope. Fields, properties, attributes on the Main method or Program class, setting the namespace, changing the class name, etc are all no longer available (the only exception is "importing" namespaces with using lines).

If that limitation doesn't work for you, don't use the feature.


For .NET 6 and higher, use dongus's answer.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • 1
    It seems that Top Level Statements and Top Level Program are used interchangeably. I can't find a meaningful difference between the terms, so if there is, please let me know. – gunr2171 Dec 23 '21 at 01:50
-1

Add this inside Program.cs

[ExcludeFromCodeCoverage]
public partial class Program { }
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 15 '23 at 00:18