小编Gui*_*75 的帖子

使用 C# 9 顶级语句时如何在 Main 范围之外添加代码?

我的理解是类似于直接把代码写到老的“ static void Main(string[] args)”中,不需要显示上面的内容。

但是,我曾经在类 Program 中声明我的变量,以便从其他类访问它们(抱歉,如果不是最佳实践,我自己学习了 C#,只要它有效,我对我的代码很满意)。请参阅下面的示例:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 C# 9,似乎我只能在 Main 部分声明变量,那么如何声明它们以便能够从其他类访问它们呢?

c# c#-9.0

13
推荐指数
2
解决办法
5231
查看次数

标签 统计

c# ×1

c#-9.0 ×1