Optional multidimensional arrays as arguments in C #

I want to declare a function with 1 mandatory argument and 4 optional arguments of a 2D array, how to do this? I know that the argument is optional, we must put a value in it at the time the function is created.

I also saw that I did it wrong and has an "Array initializers can only be used in a variable or field initializer. Try using a new expression instead."Error

private String communicateToServer(String serverHostname,
                               String[,] disk = new string[] {{"dummy","dummy"}},
                               String[,] hdd= new string[] {{"dummy","dummy"}}
                               String[,] nic= new string[] {{"dummy","dummy"}}
                               String[,] disk = new string[] {{"dummy","dummy"}}
)
+3
source share
1 answer

It is not possible to do this directly, but you can get a similar effect by running the following template

private String communicateToServer(String serverHostname,
                                   String[,] disk = null,
                                   String[,] hdd= null,
                                   String[,] nic= null) {

   disk = disk ?? new string[] {{"dummy","dummy"}},
   hdd= hdd ?? new string[] {{"dummy","dummy"}}
   nic= nic ?? new string[] {{"dummy","dummy"}}

    ...
}

null , null - , . , null .

+4

All Articles