我可以創建一個C#函數,它有2個數組作爲輸入參數,2個數組作爲輸出?具有2個數組作爲輸入參數和2個作爲輸出的函數?
3
A
回答
0
void YourFunc(Int32[] input1, Int32[] input2, out Int32[] output1, out Int32[] output2)
{
output1 = new Int32[] { 1, 2, 3 };
output2 = new Int32[] { 4, 5, 6 };
}
…
YourFunc(i1, i2, out o1, out o2);
0
您可以將2個數組作爲參數傳遞給一個函數,但函數可以返回一個單一的東西。所以你可以用你需要返回的兩個數組創建一個對象,然後返回該對象。
1
一個方法只能有一個返回值;但是,您可以使用out
參數來返回多個結果。
void MyFunction(int[] input1, int[] input2, out int[] output1, out int[] output2)
1
使用元組:
public Tuple<Object[], Object[]> FunctionName(Object[] array1, Object[] array2)
{
//Your code
return new Tuple<Object[],Object[]>({},{});
}
0
你肯定可以,老兄!
public ArrayGroup MyFunction(myType[] firstArg, myType[] secondArg) {
ArrayGroup result = new ArrayGroup();
/*Do things which fill result.ArrayOne and result.ArrayTwo */
return ArrayGroup;
}
class ArrayGroup {
myType[] ArrayOne { get; set;}
myType[] ArrayTwo { get; set;}
}
用你希望數組的任何類型填充myType!像string
或int
或複雜類型!
0
當然,你可以,開始爲響應容器:
public class Response
{
public string[] One{get;set;}
public string[] Two{get;set;}
}
而且你的方法可能看上去像
public Response DoSomething(string[] inputOne, string[] inputTwo)
{
// do some thing interesting
return new Respponse
{
One = new string[]{"Hello","World"},
Two = new string[]{"Goodbye","Cruel","World"},
}
}
0
方案一:創建型保持的結果:
SomeResult MyFunction(T[] arr1, T[] arr2)
{
// ..
return new SomeResult(r1, r2);
}
class SomeResult
{
public SomeResult(T[] a, T[] b) { /* .. */ }
// Rest of implementation...
}
選項二:Ret甕一個元組:
Tuple<T[], T[]> MyFunction(T[] arr1, T[] arr2) { }
選項三:使用了參數(不這樣做):
void MyFunction(T1[] arr1, T[] arr2, out T[] result1, out T[] result2) { }
我寧願選擇一個,並建議不要使用out參數。如果兩個參數是相同類型但不可互換我建議還爲參數創建一個類型,使其成爲具有單一結果的單個參數函數。
0
是的,你可以做到這一點!
您需要傳遞兩個輸出數組作爲該函數的參考。
這裏是代碼示例。
功能
private bool ArrayImp(string[] pArray1, string[] pArray2, ref string[] oArray1, ref string oArray2)
{
//Do your work here
//Here oArray1 and oArray2 are passed by reference so you can directly fill them
// and get back as a output.
}
函數調用
string[] iArray1 = null;
string[] iArray2 = null;
string[] oArray1 = null;
string[] oArray2 = null;
ArrayImp(iArray1, iArray2, oArray1, oArray2);
在這裏,你需要通過iArray1和iArray2作爲輸入數組,你會得到oArray1和oArray2作爲輸出。
Cheeerss !!快樂編碼!
相關問題
- 1. 將具有2個參數的函數轉換爲lambda動作
- 2. 做一個函數作爲輸入讀取只有2列
- 3. 使用函數參數作爲輸入和輸出
- 4. 作爲輸出參數的函數
- 5. 使用...函數參數作爲另一個函數的輸入
- 6. 輸出數組的值作爲參數
- 7. 布爾函數作爲輸入參數
- 8. 結構數組作爲輸入參數
- 9. 拆分數組作爲輸入參數
- 10. PSCustomObjects數組作爲輸入參數
- 11. 角2 - 通延遲組件動畫作爲輸入參數
- 12. 使用多個列表作爲函數的輸入參數(Python)
- 13. 將多個數組作爲Web API的輸入參數
- 14. Oracle函數以table_name和date作爲輸入,數字作爲輸出
- 15. 輸出一個3D數組作爲表
- 16. 如何將函數的輸出傳遞給另一個函數作爲輸入?
- 17. KSOAP 2輸入參數爲空
- 18. 作爲數組輸出
- 19. pl/pgsql數組作爲輸入到一個聚合函數
- 20. Python.scipy:Curve_fit具有多個輸入和輸出的函數
- 21. JavaScript將數組作爲參數傳遞給具有多個參數的函數
- 22. 如何將具有2個參數的函數作爲參數傳遞給函數?
- 23. 不知道如何輸入一個函數的輸出作爲另一個函數的輸入
- 24. 一個函數中Matlab可選的輸入和輸出參數
- 25. 如何將2個參數作爲Scheme中的函數?
- 26. 輸入多個變量到函數,搜索JSON數組,並接收多個數組Val作爲輸出
- 27. 傳入一個對象作爲一個輸出參數
- 28. 具有作爲參數的函數
- 29. 使用數組寫入過程作爲輸出參數
- 30. 一個動態數據作爲一個Haskell函數的輸入
歡迎來到stackoverflow,我編輯了你的問題,看看爲什麼:http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles –