.NET Framework 3.5 的元组 (.NET 4) 等效项 [英] Equivalent of Tuple (.NET 4) for .NET Framework 3.5

查看:46
本文介绍了.NET Framework 3.5 的元组 (.NET 4) 等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET Framework 3.5 中是否存在与 .NET 4 元组?

Is there a class existing in .NET Framework 3.5 that would be equivalent to the .NET 4 Tuple?

我想用它来从一个方法中返回多个值,而不是创建一个 struct.

I would like to use it in order to return several values from a method, rather than create a struct.

推荐答案

不,不在 .Net 3.5 中.但是创建自己的应该不难.

No, not in .Net 3.5. But it shouldn't be that hard to create your own.

public class Tuple<T1, T2>
{
    public T1 First { get; private set; }
    public T2 Second { get; private set; }
    internal Tuple(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

public static class Tuple
{
    public static Tuple<T1, T2> New<T1, T2>(T1 first, T2 second)
    {
        var tuple = new Tuple<T1, T2>(first, second);
        return tuple;
    }
}

更新: 将静态内容移动到静态类以允许类型推断.通过更新,您可以编写诸如 var tuple = Tuple.New(5, "hello"); 之类的内容,它会隐式地为您修复类型.

UPDATE: Moved the static stuff to a static class to allow for type inference. With the update you can write stuff like var tuple = Tuple.New(5, "hello"); and it will fix the types for you implicitly.

这篇关于.NET Framework 3.5 的元组 (.NET 4) 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆