Thanks. I knew C# didn't support named or optional parameters before .NET 4.0, but I've always thought they were part of VB.NET and hence could be used in Attributes (as long as you were working in VB) as usual. Apparently not. It's getting late so I haven't read all of it yet, I'll do that tomorrow.
Originally Posted by Sitten Spynne
Do you mean named parameters? Or specifically named parameters in attributes? Named parameters are quite common aren't they? I can see great practical use for them, as long as you have a method with a lot of optional parameters. If you have a method with, say, 10 optional parameters and you only want to specify the last one, then without named parameters you could either:
Originally Posted by JuggaloBrotha
1. Supply the default value for each parameter (so you have to look them up), or
2. Leave it empty, resulting in a method call like
Yeah... not really the syntactic sugar VB promised to have
With named parameters you just do
Much easier to read.
And even with normal methods I can see some use for them. If you are calling some method that you don't really know, I find it useful to supply the parameter names just so I can see what the various values do. For example, this:
is much easier to read at a glance then
Me.CountElements(CountStrings:=True, CountIntegers:=True, CountDates:=False, CountDoubles:=False)
unless you know exactly in which order the parameters are declared.
Me.CountElements(True, True, False, False)