Another Little C# Thing I didn't know
Sometimes I don't think I know much about C# (or anything else for that matter).
I didn't know you could do this:
static void two(string x, int num=1)
in this, the parameter "num" is optional. I didn't know you could optional parameters in C#. I've been looking at C# code since C# has been around -- code reviews, my own code, samples on the internet -- but I can't recall ever seeing anyone use this.
Here's a code sample.
static void Main(string[] args) { two("one", 2); two("two"); } static void two(string x, int num=1) { Console.WriteLine(x + " " + num); }
You can also used named parameters. So this also works:
two(num:7, x :"named");
(at least in .Net 2.0 and later).
I remember named and optional parameters in VB4,
but I was completely clueless that you could do it in C#.
Now, we're all a tiny bit less clueless.
--kevin
No comments:
Post a Comment