gpt4 book ai didi

c# - 在 C# 中用空值或空值初始化元组

转载 作者:太空狗 更新时间:2023-10-29 22:03:07 25 4
gpt4 key购买 nike

我在 SetValue() 中设置了这个字典和元组,如下所示:-

var myDict = new Dictionary<string, Tuple<string, string>>();

private void SetValue()
{
var myTuple1= Tuple.Create("ABC", "123");
var myTuple2= Tuple.Create("DEF", "456");
myDict.Add("One", myTuple1)
myDict.Add("Two", myTuple2)
}

我正在尝试检索 GetValue() 中的元组,如下所示:-

private void GetValue()
{
var myTuple = new Tuple<string, string>("",""); //Is this correct way to initialize tuple
if (myDict.TryGetValue(sdsId, out myTuple))
{
var x = myTuple.Item1;
var y = myTuple.Item2;
}
}

我的问题是,这是否是在从字典中检索元组时初始化元组的正确方法?有更好的代码吗?

 var myTuple = new Tuple<string, string>("","");

最佳答案

如果是out参数,对象在使用前不需要初始化。你应该能够做到:

Tuple<string,string> myTuple;
if (myDict.TryGetValue(sdsId, out myTuple))
{
var x = myTuple.Item1;
var y = myTuple.Item2;
}

关于c# - 在 C# 中用空值或空值初始化元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501684/

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