📜 ⬆️ ⬇️

New features in C # 4.0. Part 2: Default Parameters

Today we will talk about another new C # 4.0, which I have been waiting for for many years. In the past, its absence was due to an architectural decision. But, apparently, pragmatism has 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 will deal with the default parameters.

Suppose we have this class:
public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  1. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  2. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  3. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  4. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  5. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  6. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
  7. public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void PerformOperation( string val1, int val2, double val3) { Console .WriteLine( "{0},{1},{2}" , val1, val2, val3); } } * 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” values ​​of the parameters 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. public void PerformOperation ( string val1)
  8. {
  9. PerformOperation (val1, 10, 12.2);
  10. }
  11. public void PerformOperation ( string val1, int val2)
  12. {
  13. PerformOperation (val1, val2, 12.2);
  14. }
  15. public void PerformOperation ( string val1, int val2, double val3)
  16. {
  17. Console .WriteLine ( "{0}, {1}, {2}" , val1, val2, val3);
  18. }
  19. }
* 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 clean is it, eh? How do we call this method now? 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 good. 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 are distributed to the constructors:
  1. public class TestClass
  2. {
  3. public TestClass ( string someValue = "testValue" )
  4. {
  5. }
  6. public void PerformOperation ( string val1 = "val" , int val2 = 10, double val3 = 12.2)
  7. {
  8. Console .WriteLine ( "{0}, {1}, {2}" , val1, val2, val3);
  9. }
  10. }
* This source code was highlighted with Source Code Highlighter .
No more multiple constructor overloads!

And what happens if we omit val2 in the method call shown above? Ie we want to specify val1 and val3, but leave the default value for val2. We can't 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 will not compile, since 10.2 cannot be cast to int - here C # tries to leave the third parameter by default, not the second, as we need. So, what is the solution we have? We can use named parameters. They consist of specifying the name of the parameter, a colon, and the value that we pass. Ie 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 the change of the name of the parameter will bear 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 you have one more interesting new feature of C # 4.0 and one more reason to get accustomed to the new VS2010.

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

Crosspost from my blog

Source: https://habr.com/ru/post/43814/


All Articles