构造函数中未分配的引用值

发布于 2025-01-10 19:25:35 字数 1432 浏览 0 评论 0原文

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

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

发布评论

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

评论(2

℉絮湮 2025-01-17 19:25:35

在 C# 中,ref 关键字与类 C 指针的概念相去甚远。这是您在这个方向上可以做的最接近的事情(使用安全代码):

class Program
{
     static void Main(string[] args)
    {
        Data data = new Data();
        State stateA = new State("A", value => data.outputA = value, () => data.outputA);
        State stateB = new State("B", value => data.outputB = value, () => data.outputB);
        stateA.Output = 10;
        stateB.Output = 8;
        Console.WriteLine(data.outputA); //10
        Console.WriteLine(data.outputB); //8
        Console.ReadLine();
    }
}


public class Data
{
    public int outputA;
    public int outputB;
}
public class State
{
    public string name;
    private Action<int> _outputSetter;
    private Func<int> _outputGetter;

    public State(string _name, Action<int> _outputSetter, Func<int> _outputGetter)
    {
        this.name = _name;
        this._outputSetter = _outputSetter;
        this._outputGetter = _outputGetter;
    }


    public int Output
    {
        get => _outputGetter();
        set => _outputSetter(value);
    }
}

In c# the ref keyword is far from the concept of a C-like pointer. This is the closest thing you can do in that direction (using safe code):

class Program
{
     static void Main(string[] args)
    {
        Data data = new Data();
        State stateA = new State("A", value => data.outputA = value, () => data.outputA);
        State stateB = new State("B", value => data.outputB = value, () => data.outputB);
        stateA.Output = 10;
        stateB.Output = 8;
        Console.WriteLine(data.outputA); //10
        Console.WriteLine(data.outputB); //8
        Console.ReadLine();
    }
}


public class Data
{
    public int outputA;
    public int outputB;
}
public class State
{
    public string name;
    private Action<int> _outputSetter;
    private Func<int> _outputGetter;

    public State(string _name, Action<int> _outputSetter, Func<int> _outputGetter)
    {
        this.name = _name;
        this._outputSetter = _outputSetter;
        this._outputGetter = _outputGetter;
    }


    public int Output
    {
        get => _outputGetter();
        set => _outputSetter(value);
    }
}
你列表最软的妹 2025-01-17 19:25:35

你必须稍微纠正一下 State 类

  Data data = new Data();
    State stateA = new State("A", ref data);
    State stateB = new State("B", ref data);
    stateA.output = 10;
    stateB.output = 8;
    Console.WriteLine(data.outputA); //10
    Console.WriteLine(data.outputB); //8
    Console.ReadLine();

public class State
{
    public int output
    {
        get { return name == "A" ? address.outputA : address.outputB; }
        set { if(name == "A") address.outputA = value; else   address.outputB = value; }
    }

    public string name;
    public Data address;

    public State(string _name, ref Data _output)
    {
        this.name = _name;
        address = _output;
    }
}

you will have to correct State class a little

  Data data = new Data();
    State stateA = new State("A", ref data);
    State stateB = new State("B", ref data);
    stateA.output = 10;
    stateB.output = 8;
    Console.WriteLine(data.outputA); //10
    Console.WriteLine(data.outputB); //8
    Console.ReadLine();

public class State
{
    public int output
    {
        get { return name == "A" ? address.outputA : address.outputB; }
        set { if(name == "A") address.outputA = value; else   address.outputB = value; }
    }

    public string name;
    public Data address;

    public State(string _name, ref Data _output)
    {
        this.name = _name;
        address = _output;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文