如何在Console App .NET 6.0的program.C中声明静态实例。

发布于 2025-01-24 14:05:05 字数 1475 浏览 2 评论 0原文

我创建了一个控制台应用程序.net 6.0用于学习单例模式。 但是我已经观察到program.cs文件的重大变化。

名称空间classmain方法已从program.cs in .net 6.0 删除代码>。

我相信将在program.cs中编写的代码将被视为main方法中的代码。 但是我想创建两个使用全球式实例的静态方法,但我无法做到这一点。

通过声明下面的实例:

”在此处输入图像描述”

它给出了编译错误:

CS0106修饰符“静态”对此项目无效

CS0106在使用静态方法中的实例时, :

它给出了编译错误:

cs8421:静态本地函数不能包含引用 'TableServers1'。

这是program.cs的所有代码:

using SingletonApp;

static TableServers tableservers1 = TableServers.GetInstance();
static TableServers tableservers2 = TableServers.GetInstance();

Console.WriteLine("Hello, World!");

for (int i = 0; i < 5; i++)
{
    Host1();
    Host2();
}
Console.WriteLine();


static void Host1()
{
    Console.WriteLine("Host1 Next server is: " + tableservers1.GetNextServer());
}

static void Host2()
{
    Console.WriteLine("Host2 Next server is: " + tableservers2.GetNextServer());
}

请有人可以解释我们如何使用此新的Minified program.cs,特别是使用全局实例和变量以及创建方法。

I have created a console application .NET 6.0 for learning of Singleton Pattern.
But I have observed a significant change in Program.cs file.

namespace, class and Main method have been removed from program.cs in .NET 6.0.

I believe the code that will be written in program.cs will be considered as within Main method.
But I want to create two static methods which are using globally intialized instances but I am not able to do that.

By declaring the instances like below:

enter image description here

It gives Compile Error:

CS0106 The modifier 'static' is not valid for this item

While using the instances in static methods like below:
enter image description here

It gives Compile Error:

CS8421: A static local function cannot contain a reference to
'tableservers1'.

Here is all the code of program.cs :

using SingletonApp;

static TableServers tableservers1 = TableServers.GetInstance();
static TableServers tableservers2 = TableServers.GetInstance();

Console.WriteLine("Hello, World!");

for (int i = 0; i < 5; i++)
{
    Host1();
    Host2();
}
Console.WriteLine();


static void Host1()
{
    Console.WriteLine("Host1 Next server is: " + tableservers1.GetNextServer());
}

static void Host2()
{
    Console.WriteLine("Host2 Next server is: " + tableservers2.GetNextServer());
}

Please someone can explain how can we use this new minified program.cs, specially to use global instance and variables and creating methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2025-01-31 14:05:05

此答案因为C#10您可以通过引入公共部分类别comproge(在位置)您的底部最高级别语句文件或在单独的文件中):

// rest of your top-level statement code 
// ...
public partial class Program 
{
    static TableServers tableservers1 = TableServers.GetInstance();
    static TableServers tableservers2 = TableServers.GetInstance();
}

但是我认为切换到“旧样式” program类或将静态字段移至另一类将是一个更好的选择。

As in this answer since C# 10 you can achieve this by introducing public partial class Program (at the bottom of you top level statement file or in separate file):

// rest of your top-level statement code 
// ...
public partial class Program 
{
    static TableServers tableservers1 = TableServers.GetInstance();
    static TableServers tableservers2 = TableServers.GetInstance();
}

But I would argue that switching to "old style" Program class or moving static fields to another class would be a better option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文