将Last属性添加到System.Array的最佳方法是什么?

时间:2012-07-17 08:27:58

标签: c# .net

我尝试了下面的解决方案,但我不喜欢它。我正在寻找扩展课程,而不是封装double[]

感谢。

编辑:我们需要使用.NET 2.0

public class DoubleArray : IList, ICloneable
{

    protected double[] items;

    /// <summary>
    /// Gets the last item in the array.
    /// </summary>
    public double Last { get { return items[items.Length-1]; } }

    /// <summary>
    /// Gets the number of items in the array.
    /// </summary>
    public int Length { get { return items.Length; } }

    /// <summary>
    /// Number of items constructor.
    /// </summary>
    /// <param name="len">Number of items</param>
    public DoubleArray(int len)
    {
        items = new double[len];
    }      

    public double this[int index]
    {
        get { return items[index]; }
        set { items[index] = value; }
    }

    public bool IsReadOnly
    {
        get { return items.IsReadOnly; }
    }

    public bool IsFixedSize
    {
        get { return items.IsFixedSize; }
    }

    public void CopyTo(Array array, int index)
    {
        items.CopyTo(array, index);
    }

    public IEnumerator GetEnumerator()
    {
        return items.GetEnumerator();
    }

    public object SyncRoot
    {
        get { return items.SyncRoot; }
    }

    public bool IsSynchronized
    {
        get { return items.IsSynchronized; }
    }

    object ICloneable.Clone()
    {
        return items.Clone();
    }

    #region ICollection and IList members implementation

    // hides this members from intellisense, see: http://stackoverflow.com/questions/10110205/how-does-selectedlistviewitemcollection-implement-ilist-but-not-have-add

    int ICollection.Count
    {
        get { return items.Length; }
    }

    int IList.Add(object value)
    {
        throw new NotImplementedException();
    }

    bool IList.Contains(object value)
    {
        throw new NotImplementedException();
    }

    void IList.Clear()
    {
        throw new NotImplementedException();
    }

    int IList.IndexOf(object value)
    {
        throw new NotImplementedException();
    }

    void IList.Insert(int index, object value)
    {
        throw new NotImplementedException();
    }

    void IList.Remove(object value)
    {
        throw new NotImplementedException();
    }

    void IList.RemoveAt(int index)
    {
        throw new NotImplementedException();
    }

    object IList.this[int index]
    {
        get { throw new NotImplementedException(); }
        set { throw new NotImplementedException(); }
    } 

    #endregion

}

5 个答案:

答案 0 :(得分:2)

你不能使用LINQ而只是打电话

array.Last()

? (.NET 3.5及更高版本和“使用System.Linq;”)

.NET 2解决方案:(如果Count == 0,将触发IndexOutOfRange exc。)

public class MyList<T> : List<T>
{
    public T Last
    {
        get
        {
             return this[this.Count - 1];
        }
    }
}

答案 1 :(得分:2)

由于你不能使用Linq,扩展方法或其他只是用静态方法添加Utility类:

public static class ArrayUtility
{
    public static double Last(double[] items)
    {
        return items[items.Length -1]);
    }
}

否则,如果您不必使用array,请使用List<double>

如果您使用List<double>。致电Add(YourNewDouble)会成功。 如果您想要队列或堆栈行为。 Net有类:Queue<T>,Stack。

答案 2 :(得分:1)

这是一个满足我的解决方案。 C#3.0扩展可以在.NET 2.0中使用,只需添加以下代码:

#if NET20
namespace System.Runtime.CompilerServices
{
    [AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
     public class ExtensionAttribute : Attribute
    {
    }
}
#endif

之后您可以使用以下扩展方法(以及许多其他方法):

public static class DoubleArrayExtension
{
    // This is the extension method.
    // The first parameter takes the "this" modifier
    // and specifies the type for which the method is defined.       
    public static double Last(this double[] items)
    {
        return items[items.Length - 1];
    }

}

感谢您提供的所有提示和建议!

答案 3 :(得分:0)

使用类List或者您需要实现相同的功能

如果List不合适,请使用静态方法Array.Resize(param)Array.Copy(param)

答案 4 :(得分:0)

在文件顶部添加using System.Linq;,以便能够在代码中使用Linq表达式。 items.Last()将返回最后一个数组项。

Here is a complete Linq methods list