In Parameters in C# 7.2 – Read-only References

In Parameters in C# 7.2 – Read-only References

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.

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!

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

9 Comments to “In Parameters in C# 7.2 – Read-only References”

  1. Steve Crane

    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.

  2. Abhijit Jana Author

    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++;
    }

  3. Steve Crane

    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.

  4. Abhijit Jana Author

    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.

  5. Steve Crane

    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.

  6. navdeep

    class Program
    {
    public Program(in int x)
    {
    x++ ;
    }
    }

    but read only variable can change in a constructor .

    so how we can do that ?

Comments are closed.