C# 更改方法以返回两个具有元组的字典
您好,我有一个返回C# 更改方法以返回两个具有元组的字典,c#,C#,您好,我有一个返回 Dictionary<Text, List<string>> 字典 我希望它返回两个字典 这是我的方法: public static Dictionary<Text, List<string>> CotisationURSSAF(TnsPrimitiveSession primitiveSession, int _IndexEnCours, CTNSExercice _Exercice, bool isAjustee)
Dictionary<Text, List<string>>
字典
我希望它返回两个字典
这是我的方法:
public static Dictionary<Text, List<string>> CotisationURSSAF(TnsPrimitiveSession primitiveSession, int _IndexEnCours, CTNSExercice _Exercice, bool isAjustee)
{
Dictionary<Text, List<string>> CotisationURSSAF = new Dictionary<Text, List<string>>();
Dictionary<Text, List<string>> ElementsPrisEnCompte = new Dictionary<Text, List<string>>();
return CotisationURSSAF;
}
publicstaticdictionary-cotisaf(tnspritivesession-primitiveSession-primitiveSession,int-IndexEnCours,CTNSExercice-Exercice,bool-isAjustee)
{
Dictionary CotisationURSSAF=新字典();
字典元素sprisencompt=新字典();
返回CotisationURSSAF;
}
我尝试使用Tuple,但仍然存在问题,我的代码错误:
public Tuple<Dictionary<Text, List<string>, Text, List<string>>, Dictionary<Dictionary<Text, List<string>, Text, List<string>>> CotisationURSSAF<Text, List<string>, Text, List<string>>((TnsPrimitiveSession primitiveSession, int _IndexEnCours, CTNSExercice _Exercice, bool isAjustee))
{
Dictionary<Text, List<string>> CotisationURSSAF = new Dictionary<Text, List<string>>();
Dictionary<Text, List<string>> ElementsPrisEnCompte = new Dictionary<Text, List<string>>();
return Tuple.Create(CotisationURSSAF, ElementsPrisEnCompte);
}
公共元组元组<
词典
词典
>
请随意删除换行符
然而,如果它很难创造,它将很难阅读。也许你应该把它们放在它们自己的类中以提高可读性。Bonjour,一个解决方案:返回一个字典的数组,然后你可以通过数组[0]和数组[1]进行访问
public static Dictionary<Text, List<string>>[] CotisationURSSAF(TnsPrimitiveSession primitiveSession, int _IndexEnCours, CTNSExercice _Exercice, bool isAjustee)
{
var list = new List<Dictionary<Text, List<string>>>();
Dictionary<Text, List<string>> CotisationURSSAF = new Dictionary<Text, List<string>>();
Dictionary<Text, List<string>> ElementsPrisEnCompte = new Dictionary<Text, List<string>>();
list.Add(CotisationURSSAF);
list.Add(ElementsPrisEnCompte);
return list.ToArray();
}
publicstaticdictionary[]CotisationURSSAF(tnspritivesession primitiveSession,int-indexincours,CTNSExercice,bool-isAjustee)
{
var list=新列表();
Dictionary CotisationURSSAF=新字典();
字典元素sprisencompt=新字典();
列表。添加(CotisationURSSAF);
列表。添加(元素sprisencompt);
return list.ToArray();
}
有两种更好的方法来代替这个没有人能理解的奇怪的元组怪物:
使用具有两个属性的自定义类,这两个属性类型都是Dictionary
返回包含两项的数组:Dictionary[]
publicstaticdictionary[]CotisationURSSAF(tnspritivesession primitiveSession,int-indexincours,CTNSExercice,bool-isAjustee)
{
Dictionary CotisationURSSAF=新字典();
字典元素sprisencompt=新字典();
返回新的[]{CotisationURSSAF,elementsprisencompt};
}
您可以通过索引访问它们。方法类型必须与您的返回类型匹配。您正在定义一个Dictionary,当Dictionary只接受2时,它是4种通用数据类型
元组是另一种接受两种(或更多)通用数据类型的构造
因此,您真正想要做的是返回一个元组,其中包含两个在您的方法中定义的字典:
Tuple<Dictionary<Text, List<string>>, Dictionary<Text, List<string>>>
元组
看起来你应该研究一下泛型。我建议你选择类而不是元组。这让打电话的人看得更清楚。例如:
public class ReturnModel
{
public Dictionary<Text, List<string>> CotisationURSSAF { get; set; }
public Dictionary<Text, List<string>> ElementsPrisEnCompte { get; set; }
public ReturnModel(Dictionary<Text, List<string>> cotisationURSSAF, Dictionary<Text, List<string>> elementsPrisEnCompte)
{
this.CotisationURSSAF = cotisationURSSAF;
this.ElementsPrisEnCompte = elementsPrisEnCompte;
}
}
公共类返回模型
{
公共字典CotisationURSSAF{get;set;}
公共字典元素sprisencompt{get;set;}
公共返回模型(Dictionary cotisationURSSAF、Dictionary elementsPrisEnCompte)
{
this.CotisationURSSAF=CotisationURSSAF;
this.elementsprisencompt=elementsprisencompt;
}
}
您的方法将返回此模型。(将其重命名为您的商业案例)
快乐编码 你有点迷路了。。。请尝试使用公共元组CotisationURSSAF…
请不要将代码段用于C。使用代码块代替这个通用的元组怪物,为什么不简单地返回一个包含两个项的字典[]
?忘记元组,为自己构建一个类来保存字典,它会更直接forward@PeterBons:它的唯一职责是创建类型monsters,如果构造函数需要属性,请小心。这使您的类非常不灵活,没有任何好处,您可以轻松地执行newreturnmodel(){CotisationURSSAF=…}代码>,这更灵活,也更容易创建。现在我如何调用这个方法来获取字典的第一项这就是我所做的(dictionary notificationsURSSAF=WebTns3.Repository.TnsRepositoryExercice.CotisationURSSAF(this.PrimitiveSession,_iIndexincours,_Exercice,false);)@MECABBAZZO95:只需将此数组存储在一个变量中,然后通过数组[0]
访问CotisationURSSAF
,通过数组[1]访问元素sprisencompt
字典
@TimSchmelter如何将其存储在变量中,然后在我的view@mecabpazzo95:var bothDict=WebTns3.Repository.TnsRepositoryExercice.CotisationURSSA(…)代码>现在访问两个字典:var first=bothDict[0];var second=bothDict[1]代码>
Tuple<Dictionary<Text, List<string>>, Dictionary<Text, List<string>>>
public class ReturnModel
{
public Dictionary<Text, List<string>> CotisationURSSAF { get; set; }
public Dictionary<Text, List<string>> ElementsPrisEnCompte { get; set; }
public ReturnModel(Dictionary<Text, List<string>> cotisationURSSAF, Dictionary<Text, List<string>> elementsPrisEnCompte)
{
this.CotisationURSSAF = cotisationURSSAF;
this.ElementsPrisEnCompte = elementsPrisEnCompte;
}
}