gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:37:08 24 4
gpt4 key购买 nike

我尝试了下面的解决方案,但我不喜欢它。我正在寻找扩展类,而不是封装 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

}

最佳答案

你不能使用 LINQ 并调用

array.Last()

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

.NET 2 解决方案:(将触发 IndexOutOfRange exc. 在 Count == 0 的情况下)

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

关于c# - 将 Last 属性添加到 System.Array 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11518705/

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