Now we can instantiate it and call the method: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 .
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 .
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 .
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 .
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 .
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:* This source code was highlighted with Source Code Highlighter .
- var testClass = new TestClass ();
- testClass.PerformOperation ( "val" , 10, 12.2);
Pretty long. But C # 4.0 gives us a better solution:* This source code was highlighted with Source Code Highlighter .
- public class TestClass
- {
- public void PerformOperation ()
- {
- PerformOperation ( "val" , 10, 12.2);
- }
- public void PerformOperation ( string val1)
- {
- PerformOperation (val1, 10, 12.2);
- }
- public void PerformOperation ( string val1, int val2)
- {
- PerformOperation (val1, val2, 12.2);
- }
- public void PerformOperation ( string val1, int val2, double val3)
- {
- Console .WriteLine ( "{0}, {1}, {2}" , val1, val2, val3);
- }
- }
How clean is it, eh? How do we call this method now? Yes, exactly the same, if it were an overload:* This source code was highlighted with Source Code Highlighter .
- public class TestClass
- {
- public void PerformOperation ( string val1 = "val" , int val2 = 10, double val3 = 12.2)
- {
- Console .WriteLine ( "{0}, {1}, {2}" , val1, val2, val3);
- }
- }
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:* This source code was highlighted with Source Code Highlighter .
- var testClass = new TestClass ();
- testClass.PerformOperation ( "val" , 10);
No more multiple constructor overloads!* This source code was highlighted with Source Code Highlighter .
- public class TestClass
- {
- public TestClass ( string someValue = "testValue" )
- {
- }
- public void PerformOperation ( string val1 = "val" , int val2 = 10, double val3 = 12.2)
- {
- Console .WriteLine ( "{0}, {1}, {2}" , val1, val2, val3);
- }
- }
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:* This source code was highlighted with Source Code Highlighter .
- var testClass = new TestClass ();
- testClass.PerformOperation ( "val" , 10.2);
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.* This source code was highlighted with Source Code Highlighter .
- var testClass = new TestClass ();
- testClass.PerformOperation ( "val" , val3: 10.2);
Source: https://habr.com/ru/post/43814/
All Articles