gpt4 book ai didi

c# - 构造函数有 2 个参数,但使用 1 个参数调用

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:07 25 4
gpt4 key购买 nike

我有以下类(class):

public class Notifications
{
public static Notifications Instance = new Notifications();

public NotificationHubClient Hub { get; set; }

private Notifications() {
Hub = NotificationHubClient.CreateClientFromConnectionString(
"<conn string with full access>", "<hub name>");
}
}

我试图修改代码以将连接字符串的值和集线器的名称动态传递给构造函数,我将构造函数更改为:

private Notifications(string connectionString,string hubName) {...}

我现在遇到的问题是 Instance ,因为您可以注意到它正在调用一个没有参数的构造函数,我试图更改它但没有成功:

public static Notifications Instance = new Notifications(
string connectionString,string hubName);

我应该怎么做才能使 Instance 调用构造函数并传递这两个参数?

最佳答案

Instance 无权访问这些参数值 - 它必须为它们提供值!

因此您可以执行以下操作:

public static Notifications Instance = new Notifications("<conn string with full access>", "<hub name>");

关于c# - 构造函数有 2 个参数,但使用 1 个参数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com