In Parameters in C# 7.2 – Read-only References

By | November 27, 2017

In one of our recent post, we discussed about – What is the difference between Ref and Out Keyword in C#?  We have seen both the out and ref are used to returning values and both passes variables by reference only. Out parameter is required to be modified by the method. On the other hand,’s ref allows to modify 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 in side the method. C# 7.2 introduced a new parameter passing mode called in parameter.


In parameter are like ref parameter only, except they are read-only inside the method.  You can only refer them, they can’t be modified further.

        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!

Author: 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.

7 thoughts on “In Parameters in C# 7.2 – Read-only References

  1. Pingback: Dew Drop - November 27, 2017 (#2611) - Morning Dew

  2. 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.

  3. Abhijit Jana Post 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++;
    }

  4. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2474

  5. 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.

  6. Abhijit Jana Post 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.

  7. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verify Captcha * Time limit is exhausted. Please reload the CAPTCHA.