New features in C # 4.0. Part 2: default settings

Original author: Justin Etheredge
  • Transfer
Today we’ll talk about another new C # 4.0, which I have been waiting for many years. In the past, its absence was due to an architectural solution. But, apparently, pragmatism won and now we have the default settings. To make them even more useful, they added named parameters to them. We will discuss them in a couple of minutes, and now we’ll deal with the default settings.

Suppose we have a class like this:
  1. public class TestClass
  2. {
  3.   public void PerformOperation(string val1, int val2, double val3)
  4.   {
  5.     Console.WriteLine("{0},{1},{2}", val1, val2, val3);
  6.   }
  7. }
* This source code was highlighted with Source Code Highlighter.
Now we can instantiate it and call the method:
  1. var testClass = new TestClass();
  2. testClass.PerformOperation("val", 10, 12.2);
* This source code was highlighted with Source Code Highlighter.
But, what if we know the “good” parameter values ​​that are used most often? To this day, the solution was to overload the methods:
  1. public class TestClass
  2. {
  3.   public void PerformOperation()
  4.   {
  5.     PerformOperation("val", 10, 12.2);
  6.   }
  7.   
  8.   public void PerformOperation(string val1)
  9.   {
  10.     PerformOperation(val1, 10, 12.2);
  11.   }
  12.  
  13.   public void PerformOperation(string val1, int val2)
  14.   {
  15.     PerformOperation(val1, val2, 12.2);
  16.   }
  17.  
  18.   public void PerformOperation(string val1, int val2, double val3)
  19.   {
  20.     Console.WriteLine("{0},{1},{2}", val1, val2, val3);
  21.   }
  22. }
* This source code was highlighted with Source Code Highlighter.
Pretty long. But C # 4.0 gives us a better solution:
  1. public class TestClass
  2. {
  3.   public void PerformOperation(string val1 = "val", int val2 = 10, double val3 = 12.2)
  4.   {
  5.     Console.WriteLine("{0},{1},{2}", val1, val2, val3);
  6.   }
  7. }
* This source code was highlighted with Source Code Highlighter.
How much cleaner is it, huh? How do we now call this method? Yes, exactly the same if it were an overload:
  1. var testClass = new TestClass();
  2. testClass.PerformOperation("val", 10);
* This source code was highlighted with Source Code Highlighter.
Very well. The third parameter in this call is equal to 12.2 by default. Now VB.NET developers will stop laughing at us. Moreover, the default parameters apply to constructors:
  1. public class TestClass
  2. {
  3.   public TestClass(string someValue = "testValue")
  4.   {
  5.   }
  6.  
  7.   public void PerformOperation(string val1 = "val", int val2 = 10, double val3 = 12.2)
  8.   {
  9.     Console.WriteLine("{0},{1},{2}", val1, val2, val3);
  10.   }
  11. }
* This source code was highlighted with Source Code Highlighter.
No more multiple constructor overloads!

And what happens if we omit the value of val2 in the method call shown above? That is, we want to specify val1 and val3, but leave the default value for val2. We cannot do it like this:
  1. var testClass = new TestClass();
  2. testClass.PerformOperation("val", 10.2);
* This source code was highlighted with Source Code Highlighter.
This code does not compile, since 10.2 cannot be cast to int - here C # tries to leave the third parameter by default, and not the second, as we need. So what is our way out? We can use named parameters. They consist of specifying the parameter name, a colon, and the value that we pass. That is, the call will look like this:
  1. var testClass = new TestClass();
  2. testClass.PerformOperation("val", val3: 10.2);
* This source code was highlighted with Source Code Highlighter.
Quite neatly, but I am confused by the fact that now changing the parameter name will have such cardinal consequences. I think only time will tell how convenient it is in developing large applications. Although, people working with other languages ​​have been living with this for many years.

Here is another interesting new feature of C # 4.0 and another reason to look closely at the new VS2010.

Article translation: C # 4.0 New Features Part 2 - default and named parameters

Crosspost from my blog


Also popular now: