My current issue is that I cannot use the method I wrote in the way I expected I could.
This is my signature :
IList<FileInfo> GetFiles(string name = "*", params SignalState[] states);
If relevant, please note that SignalState is an enum
.
I would expect to be able to call this method with either one argument (string
OR SignalState
), none, or both of them. It works if I use both arguments, if I use none of them, or if I use only the name.
Right now I would expect that only providing a SignalState
would work, because the compiler would assume the first parameter is optional and my SignalState
fits the second parameter of the signature.
It does not work, and the error is quite clear, its expecting a name.
So I have tried changing the signature to this :
IList<FileInfo> GetFiles([Optional] string name, params SignalState[] states);
and obviously add a bit of logic to handle my wildcard default value for the string, but I still face the exact same problem.
I have also tried switching the two paramters, params SignalState
first and the string last, but the compiler specifically says params should be last and shows an error in the signature.
Could someone explain to me why it is behaving the way it is ? I would expect to be able to jump to the second param since the first one is optional.
Is my only solution to implement as many overloads as necessary to be able to call my method with only a SignalState
? Or am I missing something in how optional parameters work ?