Does .net Have An Equivalent Of **kwargs In Python?
I haven't been able to find the answer to this question through the typical channels. In Python I could have the following function definition def do_the_needful(**kwargs): # K
Solution 1:
What you look for is called a variadic function. If you want to know how to implement it in various programming languages, the best is to look at the Wikipedia page about it.
So, according to the Wikipedia, implementing a variadic function in C# and VisualBasic is done like this:
Other languages, such as C#, VB.net, and Java use a different approach—they just allow a variable number of arguments of the same (super)type to be passed to a variadic function. Inside the method they are simply collected in an array.
C# Example
publicstaticvoidPrintSpaced(params Object[] objects) { foreach (Object o in objects) Console.Write(o + " "); } // Can be used to print: PrintSpaced(1, 2, "three");
VB.Net example
PublicSharedSub PrintSpaced(ParamArray objects AsObject()) ForEach o AsObjectIn objects Console.Write(o & " ") NextEndSub
' Can be used to print: PrintSpaced(1, 2, "three")
Post a Comment for "Does .net Have An Equivalent Of **kwargs In Python?"