2017-03-14 23 views
5

我有這兩個功能使用參數的String []在簽名重載函數

public static string Function1 (string id, params string[]) 
    { 
     return Function1(id, null, null, params) 
    } 

public static string Function1 (string id, string id2, Object a, params string[]) 
{ 
     string id = id, 
     if (IsValidId(id)) 
     { 
      start = new ProcessStartInfo(); 
      start.Arguments = params; 
      if (string.IsNullOrEmpty(id2)==false) 
      { 
       start.RedirectStandardOutput = true; 
      } 
     } 
} 

我想,當我做以下調用

MyStaticClaass.Function1(
     input1, 
     input2, 
     null, // (This one represents the Object) 
     input3, 
     input4, 
     input5); 

使用第二個重載有沒有一種方法,我可以強迫它進入方法的第二個定義?

編譯錯誤我是:該調用如下方法或屬性之間曖昧:(然後上述兩種方式)

PS:我還沒有選擇具有這兩種功能。我不能改變他們的簽名或他們的名字。

+0

希望你正在尋找重載,而不是重寫 –

+0

我的意思是說超載......編輯帖子:) – user3587624

+3

在你的例子中,將使用第二個定義*。 – Rob

回答

6

您可以使用Named arguments重載一個特定的功能就像

Program.Function1(
     id: input1, 
     id2: input2, 
     o:null, 
     array: new string[] {input3, 
      input4, 
      input5}); 

,它會擊中功能

public static string Function1 (string id, string id2, Object o, params string[] array) 
{ 

} 

更多的細節,你可以檢查此Named Arguments

+0

謝謝!把它作爲一個有效的答案:) – user3587624

1

它可以像這也是:

var result = Rootobject.Function1(
       "", 
       "", 
       null, // (This one represents the Object) 
       new string[] { "", "", ""}); 

但是,既然你已經知道這是一個脆弱的設計,或者你不會在這裏詢問它,也許你應該重新考慮你的過載。當然,我們有它的工作,但它不是一個好設計。