gpt4 book ai didi

c# - 通过 ref 获取字典

转载 作者:行者123 更新时间:2023-11-30 19:01:41 27 4
gpt4 key购买 nike

我想知道字典的正确值是否可以通过 ref。所以,当我改变字典的值时,其他一些值也会改变例如:

double num = 7;
Dictionary<string,double> myDict = new Dictionary<string, double>();
myDict.Add("first",num);
myDict["first"]=4;
// i want num also to change to 4.. is it possible?

感谢您的帮助。

最佳答案

不,那基本上是不可能的。如果你想要这样的东西,你需要一个包装类类型,例如

public class Wrapper<T>
{
public T Value { get; set; }
}

然后你可以使用:

var wrapper = new Wrapper<double> { Value = 7.0 };
var dictionary = new Dictionary<string, Wrapper<double>>();
dictionary.Add("first", wrapper);
dictionary["first"].Value = 4;
Console.WriteLine(wrapper.Value); // 4.0

关于c# - 通过 ref 获取字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23426915/

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