In the previous post, you learnt about – What is the difference between Ref and Out Keyword in C#? Both, the out and ref are used to return values. And, both pass variables by reference only. Also, Out parameter is required to be modified by the method. However, on the other hand,’s ref allows modifying the original values, which is always may not like to be the case. Like, we want to pass the value as references, but we want this should not be modified inside the method. C# 7.2 introduced a new parameter passing mode called in parameter.
In Parameters in C#
In parameters in C# are like ref parameter only, except they are read-only inside the method. And, they can’t be modified further. You can only refer them.
static void Main(string[] args) { int num = 20; Console.WriteLine(num); MyMethodIn(num); num++; Console.WriteLine(num); } public static void MyMethodIn(in int x) { int y = x; Console.WriteLine(y); y++; Console.WriteLine(y); Console.WriteLine(x); }
In the above example, we passed variable num as in parameter, the value is used by the MyMethodIn() but it does not have any modification and used as readonly.
In case you tried to modify the in parameter, you will get an error – Cannot assign to variable “in int” because it is a read-only variable.
public static void MyMethodIn(in int x) { int y = x++; }
Cannot assign to variable “in int” because it is a read-only variable.
To summarize, In parameter is useful when you want to pass variables as a reference but you don’t want it to be modified further by the method. Ref parameter does allow this modification, and out parameter is required to be modified.
Hope this helps!
Pingback: Dew Drop - November 27, 2017 (#2611) - Morning Dew
I’m not sure I see the point of your example. If you’re passing a value type like int then surely just passing by value as usual would have the same effect.
Thank you Steve. When the references are passed as ‘in’ it won’t let you modify further.
As showed in the post, below execution would give an error – Cannot assign to variable “in int” because it is a read-only variable.
public static void MyMethodIn(in int x)
{
int y = x++;
}
Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2474
I understand that but what I’m failing to see is what the benefit of this might be in your example over passing the int by value. Both prevent the side effect of the original passed value being modified. What I’m missing is what benefit there is to the ref readonly preventing the parameter from being modified inside the method other than preventing a side effect.
Thanks again Steve. With regards to the given example, may be not much benefit, it just showed what in does. If you consider with respect to design, having ‘in’ parameter in place, we may avoid unnecessary copying of values. Kind of design intent I would say. Value types are copied when it passed to a method, with having in modifier, it pass it as by reference, which intended to have this as reference to avoid unnecessary copying further along with restrict the modification. You may refer to this link for some more insights of this feature.
Thanks for clearing that up. So it is intended as a performance feature rather than to give control over what a method does with a copied (i.e. passed by value) parameter. Makes sense.
Pingback: Week Reading: 12/15/2017 – jbcedge
class Program
{
public Program(in int x)
{
x++ ;
}
}
but read only variable can change in a constructor .
so how we can do that ?