2012-02-23 54 views
0

我想通過使用方法來初始化一個新創建的空數組。下面的例子。 這是我想要創建的代碼的結構。c#通過使用方法初始化某些特定數組

var v = exClass[5,5]; 
v.ExtensionMethodThatWillInitilize(); 

我想ExtensionMethodThatWIllInitilize做的是以下幾點:

for(int y = 0 ; y < exClass.getLength(0); y++) { 
for(int x = 0 ; x < exClass.getLength(1); x++) { 
v[y,x] = new instanceObject(); 
}} 

於是我想出了下面的代碼...

namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     var oldEx = new ExClass[10, 10]; 
     var newEx = oldEx.init(); 
    } 
} 
public class ExClass 
{ 
    public string exString 
    public ExClass() { 
    exString = " I AM NEW! YES! "; 
} 
} 
public static class tools 
{ 
    static public ExClass[,] init(this ExClass[,] start) 
    { 
     var newArray = new ExClass[start.GetLength(0), start.GetLength(1)]; 
     for (int y = 0; y < start.GetLength(0); y++) 
     { 
      for (int x = 0; x < start.GetLength(1); x++) 
      { 
       newArray[y, x] = new ExClass(); 
      } 
     } 
     return newArray; 
    } 
} 

然而,問題是,我不知道如何修改我的init方法,以便它可以接受任何類型的數組並返回已初始化的相應數組。 我試圖使用泛型類型T,但我缺乏經驗似乎失敗。

綜述:

1)你怎麼來了,這將在一個單一的步驟初始化一個空數組的方法。

2)你如何確保該方法可以初始化任何類型的數組(假設數組中的元素有一個構造函數不帶任何參數)

+0

使用動態數組{}或{,} – MethodMan 2012-02-23 18:20:25

+0

嘗試在通用方法中發佈您的嘗試,因爲這肯定是對此的解決方案。我很確定你錯了哪裏,但最好不要猜測。 – Servy 2012-02-23 18:21:05

回答

1

泛型做的工作。您需要將您的方法聲明爲通用類型。

我已經修改了上面的例子如下:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var oldEx = new ExClass[10, 10]; 
     var newEx = oldEx.init<ExClass>(); 
    } 
} 
public class ExClass 
{ 
    public string exString = "I AM NEW"; 
} 
public static class tools 
{ 
    static public T[,] init<T>(this T[,] start) 
    { 
     var newArray = new T[start.GetLength(0), start.GetLength(1)]; 
     for (int y = 0; y < start.GetLength(0); y++) 
     { 
      for (int x = 0; x < start.GetLength(1); x++) 
      { 
       newArray[y, x] = Activator.CreateInstance<T>(); 
      } 
     } 
     return newArray; 
    } 
} 

作爲一個答案,下面您的評論:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var oldEx = tools.init<ExClass>(10, 10); 

    } 
} 
public class ExClass 
{ 
    public string exString = "I AM NEW"; 
} 
public static class tools 
{ 
    static public T[,] init<T>(int x,int y) 
    { 
     var newArray = new T[x, y]; 
     for (int i = 0; i < x; i++) 
     { 
      for (int j = 0; j < y; j++) 
      { 
       newArray[i, j] = Activator.CreateInstance<T>(); 
      } 
     } 
     return newArray; 
    } 
} 
+0

是否可以讓init方法直接修改原來的'start'? – BlueBug 2012-02-23 18:25:32

+0

這樣我就不需要重新分配整個事情了? – BlueBug 2012-02-23 18:25:46

+0

+1投票,我會盡量發佈我知道的更多細節,但我必須儘快上課!非常感謝你的修改! – BlueBug 2012-02-23 18:27:01

1

看看這個泛型方法,它使用new泛型約束:

static public void Init<T>(this T[,] array) where T : new() 
{ 
    for (int y = 0; y < array.GetLength(0); y++) 
    { 
     for (int x = 0; x < array.GetLength(1); x++) 
     { 
      array[y, x] = new T(); 
     } 
    } 
    } 

從您的代碼的主要區別是:

1)的方法是初始化指定一個泛型參數,並有where T : new()約束可以確保,而不是返回一個新的,初始化數組,我們可以稱之爲new T();

2) ,我只是簡單地初始化陣列。似乎更合乎邏輯。如果你願意,你可以使用你的原始版本,但我不明白爲什麼你需要。

+0

非常感謝你爲這個版本的代碼。我非常樂意用這種方法初始化我的數組!你的代碼將永遠存在於我的擴展類中! – BlueBug 2012-02-24 06:58:07