2012-07-17 115 views
2

我嘗試了下面的解決方案,但我不喜歡它。我正在尋找擴展類,而不是封裝double[]將Last屬性添加到System.Array的最佳方法是什麼?

謝謝。

編輯:我們需要.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 

} 
+0

作爲一個實現,我認爲這將是迄今爲止最簡單的解決方案。你還有什麼可以得到最後的財產?我想,你可以單獨追蹤最後一個值,方法是通過修改底層數組的所有方法來捕獲最後一個值,但這樣做的工作量太大或太少,無法獲得增益。這裏的實現可能與你將要使用數組給你的工作一樣好。 – Rup 2012-07-17 08:40:04

+1

此方法的一個缺點是無法將DoubleArray類作爲參數傳遞給接受'double []'的方法。 – abenci 2012-07-17 09:42:39

回答

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]; 
    } 

} 

感謝您的提示和建議夥計們!

0

使用類List工作或你需要實現相同功能的

如果List不適合使用靜態methos Array.Resize(param)Array.Copy(param)

2

廣東話你使用LINQ,只需撥打

array.Last() 

? (.NET 3.5以上和 「使用System.Linq的;」)

.NET 2解決方法:(將觸發IndexOutOfRange EXC在計數的情況下== 0)

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

忘了提及我們需要使用.NET 2.0 ... – abenci 2012-07-17 08:33:15

+0

不回答這個問題,因爲它沒有顯示如何將它添加到數組中。 – 2012-07-17 08:33:18

+0

你不能追加到數組。如果你需要添加,你最終實現自己的名單。 – 2012-07-17 08:43:01

2

,因爲你不能使用的LINQ,擴展方法等只是添加的實用工具類用靜態方法:

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

否則,如果你沒有使用一個array,只是使用List<double>

如果你去使用List<double>。呼籲Add(YourNewDouble)將使它。 如果您想要隊列或堆棧行爲。網有類:Queue<T>,堆棧。

+0

是的,我也得出了同樣的結論。 – abenci 2012-07-17 09:17:21

+0

擴展方法首先在.NET 3.0中 – 2012-07-17 09:39:25

+0

你能舉個例子嗎? – abenci 2012-07-17 09:41:18

相關問題