如何避免频繁创建临时对象

1.可以使用Tuple

2.自定义元祖类

[Serializable]
public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
    // Fields
    private T1 m_Item1;
    private T2 m_Item2;

    // Methods
    public Tuple(T1 item1, T2 item2)
    {
        this.m_Item1 = item1;
        this.m_Item2 = item2;
    }


    string ITuple.ToString(StringBuilder sb)
    {
        sb.Append(this.m_Item1);
        sb.Append(", ");
        sb.Append(this.m_Item2);
        sb.Append(")");
        return sb.ToString();
    }

    int ITuple.Size
    {
        get
        {
            return 2;
        }
    }

    // Properties
    public T1 Item1
    {
        get
        {
            return this.m_Item1;
        }
    }

    public T2 Item2
    {
        get
        {
            return this.m_Item2;
        }
    }

    //More and more...
}

  

posted @ 2019-08-06 15:16  木狼  阅读(442)  评论(0编辑  收藏  举报