This article explains the “in” parameter method overloading with Visual Studio 2017 Version 15.6. Preview 2.
The new version of Visual Studio 2017 (Version 15.6 Preview 2.0) was announced on January 10, 2018. In this version, one tiebreaker change has been done for C# Compiler which allows method calling without any conflict for the scenarios where we have multiple method overloads that differ only in parameter modifiers keywords like ‘in’.
Before jumping to the actual change, let’s try to understand the problem.
We know that we can define an overloaded method using parameter modifiers ‘in’, ‘out’ or ‘ref’. Thus, the following code is valid.
- public void DoSomething(int x) { }
- public void DoSomething(ref int x) { }
But the below code snippet will give an error.
- public void DoSomething(int x) { }
- public void DoSomething(in int x) { }
- public void DoSomething(out int x) { x = 20; }
- public void DoSomething(ref int x) { }
Error 1
'Program' cannot define an overloaded method that differs only on parameter modifiers 'out' and 'in'
Error 2
'Program' cannot define an overloaded method that differs only on parameter modifiers 'ref' and 'in'
So far, we have seen how overloading can be done using ‘in’, ‘out’ and ‘ref’ keyword. Now, let’s concentrate only on ‘in’ parameter.
While calling the method, “in” keyword is optional. Have a look at the following 2 code snippets.
Code Snippet 1
- class Program
- {
- static void Main(string[] args)
- {
- Program program = new Program();
- int number = 50;
- program.DoSomething(number);
- }
-
- public void DoSomething(in int x)
- {
-
- }
- }
Code Snippet 2
- class Program
- {
- static void Main(string[] args)
- {
- Program program = new Program();
- int number = 50;
- program.DoSomething(in number);
- }
-
- public void DoSomething(in int x)
- {
-
- }
- }
As you can see in “Code Snippet 1”, I am calling DoSomething() method without “in” keyword and in “Code Snippet 2” calling DoSomething() method with “in” keyword. However, both are calling the same method.
public void DoSomething(in int x){}
Now, overload the DoSomething() method add one more DoSomething() method in code snippet 2 as shown in the following code snippet.
Code Snippet 3
- class Program
- {
- static void Main(string[] args)
- {
- Program program = new Program();
- int number = 50;
- program.DoSomething(in number);
- }
-
- public void DoSomething(in int x)
- {
-
- }
-
- public void DoSomething(int x)
- {
-
- }
- }
Still, there is no issue, and you can compile this program successfully. Now, remove the “in” keyword from the calling method and try to call the overloaded DoSomething() method.
Before Visual Studio 2017 15.6 Preview 2, you will get the following error:
The call is ambiguous between the following methods or properties: 'Program.DoSomething(in int)' and 'Program.DoSomething(int)'
Screenshot from Visual Studio 2017 Version 15.5.3
Now, run the code with Visual Studio 2017 Version 15.6 Preview 2, and you can see that it gets compiled successfully.
Screenshot from Visual Studio 2017 Version 15.6 Preview 2
Thus, we can say that the tiebreaker change has been implemented and now it will work more efficiently. In the above screenshot, we are not passing in keyword so it will call the method
public void DoSomething(int x) { }
However, it does not mean that “in” parameter is not optional. “In” parameter is still optional insofar as there is no other matching overloaded method available.
In this article, I have just explained the tiebreaker change for “in” parameter; if you would like to explore more about “in”, “out” & “ref”, then please go through these articles.
Explore more about C# latest features,