gpt4 book ai didi

c#-3.0 - C# 3.0 元组等价物(适用于穷人)

转载 作者:行者123 更新时间:2023-12-04 10:45:50 25 4
gpt4 key购买 nike

我发现自己偶尔会在 C# 3.0 中寻找模拟元组概念的方法。随着时间的推移,我有各种“穷人”的实现,这里有一些:

基本对象数组 :

object[] poorTuple = new object[]{foo,bar,baz}; // basic object array

强类型 , 哈哈哈...
KeyValuePair<TypeA, KeyValuePair<TypeB, TypeC>> poorTuple;

实现一个类 可以使用类型推断(从 Functional Programming for the Real World 提升)
public static class Tuple{
public static Tuple<T1, T2> Create<T1 foo, T2 bar>{
return new Tuple<T1, T2>(foo, bar);
}
}
// later:
var data = Tuple.Create("foo", 42);

问题:
  • 在 C# 3.0(或缺少数据结构的选择语言)中使用穷人元组的任何其他方法。
  • 在 C# 3.0 中获取元组的最佳方法是什么 - 如果有人有库推荐,欢迎提供。
  • 在什么时候(是的,为我概括)创建特定类型而不是列表或元组之类的东西有意义? (寻找经验法则)
  • 最佳答案

    您可以创建功能类似于元组的匿名类型,除了有用的名称:

    var itemsWithChildCounts 
    = myObjects.Select(x => new { Name = x.Name, Count = x.Children.Count() });

    关于c#-3.0 - C# 3.0 元组等价物(适用于穷人),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1822687/

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