C# 7.2 - In Parameter And Performance

In C# 7.2, “in parameter” has been introduced which allows passing read-only reference of a variable. Before C# 7.2, we used “ref” and “out” keywords for passing the references of a variable. “Out” is meant for output only whereas ‘ref’ is meant for input and output both. However, if we had to pass a read-only reference, i.e., passing a variable as input only, then there was no direct option for that. So, in C# 7.2, “in parameter” has been introduced for this purpose.

C# 7.2

In my previous article, I have already discussed ‘ref read-only.’ However, in the later releases of Visual Studio 2017 version 15.5, a new parameter “in parameter” has been introduced to explain the intent in a better way.

Using “in” parameter with a value type variable.

In my previous article, I used the following code snippet for ‘ref read-only’ parameter.

  1. static void Main(string[] args)  
  2. {  
  3.     int a = 20, b = 30, c = 20;  
  4.     Write($"Sum of {a} and {b} and {c} is: ");  
  5.     Add(a, b, ref c);  
  6.     WriteLine($"{c}");  
  7. }  
  8. public static void Add(ref readonly int x, ref readonly int y, ref int z)  
  9. {  
  10.     z = x + y + z;  
  11. }  

However, it will not work with later releases of Visual Studio 2017 version 15.5 or above versions (except Visual Studio 2017 version 15.5 Preview 1).

Now, just replace the keyword “ref read-only” by “in” and recompile the code. The code snippet after replacing “ref read-only” by “in” will look like below.

  1. static void Main(string[] args)  
  2. {  
  3.     int a = 20, b = 30, c = 20;  
  4.     Write($"Sum of {a} and {b} and {c} is: ");      
  5.     Add(a, b, ref c);  
  6.     WriteLine($"{c}");  
  7. }  
  8. public static void Add(in int x, in int y, ref int z)  
  9. {  
  10.     z = x + y + z;  
  11. }  

Now, it will work successfully. Following is the output for the same.

C# 7.2

Using “in” parameter with a reference type variable.

If you are using “in” with reference type variable, then you cannot reallocate a new memory for that object, but their properties can be modified.

C# 7.2

Changing Properties Value of Reference Type Variable passed with In Parameter

C# 7.2

So, you can see in the above screenshot that I can assign a value for the properties of a reference type variable which has been passed as a reference using “in” parameter but we can not re-allocate a new memory for that object. Following is the complete code for the same.

  1. using static System.Console;  
  2. namespace InParameterWithReferenceType  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Product product = new Product();  
  9.             Modify(in product);  
  10.         }  
  11.         public static void Modify(in Product product)  
  12.         {  
  13.             //product = new Product();  
  14.             product.ProductId = 101;  
  15.             product.ProductName = "Laptop";  
  16.             product.Price = 60000;  
  17.             WriteLine($"Id: {product.ProductId} Name: {product.ProductName} Price: {product.Price}");  
  18.         }  
  19.     }  
  20.     class Product  
  21.     {  
  22.         public int ProductId { get; set; }  
  23.         public string ProductName { get; set; }  
  24.         public decimal Price { get; set; }  
  25.     }  
  26. }  

However, if I change the object from the reference type to value type, then I would not be able to assign any property value. This means in case of value type, the member of that object also becomes read-only.

Have a look at the below code.

  1. using static System.Console;  
  2. namespace InParameterWithReferenceType  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Product product = new Product();  
  9.             Modify(in product);  
  10.         }  
  11.         public static void Modify(in Product product)  
  12.         {  
  13.             //product = new Product();  
  14.             product.ProductId = 101;  
  15.             product.ProductName = "Laptop";  
  16.             product.Price = 60000;  
  17.             WriteLine($"Id: {product.ProductId} Name: {product.ProductName} Price: {product.Price}");  
  18.         }  
  19.     }  
  20.     struct Product  
  21.     {  
  22.         public int ProductId { get; set; }  
  23.         public string ProductName { get; set; }  
  24.         public decimal Price { get; set; }  
  25.     }  
  26. }  

It will give a compile time error.

C# 7.2

Comparison table for In, out and ref Parameter

in out ref
Can be used to pass a variable as a reference (value type and reference type both). Can be used to pass a variable as a reference (value type and reference type both). Can be used to pass a variable as a reference (value type and reference type both).
Only for input not for output. Only for output not for input. For input and output both.
"in variable" must be initialized before passing it as a parameter. Do not require initialization before passing it as a parameter. ref variable must be initialized before passing it as a parameter.
Cannot be declared inline. Can be declared inline. Cannot be declared inline.
"in" keyword is optional while calling the method. "out" keyword is mandatory while calling the method. "ref" keyword is mandatory while calling the method.
It is not necessary to use them in a variable before leaving the control. It is necessary to initialize out variable before leaving the control. It is not necessary to use the ref variable before leaving the control.

The above table is just a comparison for ref, in, and out variables but in C# 7.2, a lot more capabilities have been provided to ref keyword, and its usage area has been expanded to a great extent which needs a dedicated article for the same. So, I will be coming with another article for the new enhancements appended to the ref keyword in C# 7.0 and C# 7.2.

Performance using ‘in’ Parameter

You may be wondering why I am passing a variable as a read-only reference. The reason is that if I am passing a value type variable without reference then each time a new copy will be created. So, it will take extra memory and performance will be slower.

Thus, in another way, we can say that instead of passing a copy of value type, we can pass a read-only reference of a value type variable which will make it faster and will use less memory.

For Loop Without In Parameter

Let’s have a look at the below code snippet:

In the below snippet, you can see that I am using a struct with 500 properties. It is not recommended to use a struct for the large object, but here I am using it just for showing you the performance difference.

  1. namespace INPerformance  
  2. {     
  3.  class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             BigAstroObject bigAstroObject = new BigAstroObject();  
  8.      bigAstroObject.A001 = 656371600.3042M;  
  9.             bigAstroObject.A002 = 876120136.2188M;  
  10.      ……………………………………………………………………………………………  
  11.      bigAstroObject.A499 = 549329194.3128M;  
  12.             bigAstroObject.A500 = 799448442.2474M;  
  13.             for (decimal i = 0; i < 200_000_000; i++)  
  14.             {  
  15.                 DoSomeThing(bigAstroObject);  
  16.             }  
  17.         }  
  18.   
  19.         private static int DoSomeThing(BigAstroObject bigAstroObject)  
  20.         {  
  21.             //your logic  
  22.             return 0;  
  23.         }  
  24.     }  
  25.     struct BigAstroObject  
  26.     {  
  27.         public decimal A001 { get; set; }  
  28.         public decimal A002 { get; set; }  
  29.  ……………………………………………………………………………………  
  30.         public decimal A499 { get; set; }  
  31.         public decimal A500 { get; set; }  
  32.     }  
  33. }  

Refer the attachment “ForLoopWithOutInParameter.cs” for complete code of above code snippet.

Following is the snapshot for the performance of above program which is not using the “in” parameter. It means each time it is passing a new copy of the variable.

C# 7.2

For Loop With In Parameter

Now, run the same code using “in” parameter. Below is the code snippet of the same.

  1. namespace INPerformance  
  2. {     
  3.  class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             BigAstroObject bigAstroObject = new BigAstroObject();  
  8.      bigAstroObject.A001 = 656371600.3042M;  
  9.             bigAstroObject.A002 = 876120136.2188M;  
  10.      ……………………………………………………………………………………………  
  11.      bigAstroObject.A499 = 549329194.3128M;  
  12.             bigAstroObject.A500 = 799448442.2474M;  
  13.             for (decimal i = 0; i < 200_000_000; i++)  
  14.             {  
  15.                 DoSomeThing(in bigAstroObject);  
  16.             }  
  17.         }  
  18.   
  19.         private static int DoSomeThing(in BigAstroObject bigAstroObject)  
  20.         {  
  21.             //your logic  
  22.             return 0;  
  23.         }  
  24.     }  
  25.     struct BigAstroObject  
  26.     {  
  27.         public decimal A001 { get; set; }  
  28.         public decimal A002 { get; set; }  
  29.  ……………………………………………………………………………………  
  30.         public decimal A499 { get; set; }  
  31.         public decimal A500 { get; set; }  
  32.     }  
  33. }  

Refer the attachment “ForLoopWithInParameter.cs” for complete code of the above code snippet.

Following is the snapshot of the performance of the above program which is using the “in” parameter. It means each time it is not passing a new copy of the variable, rather, it is passing a read-only reference of the same copy.

C# 7.2

Thus, you can compare and see in the above screenshots what is the performance difference if I am using the “in” parameter. This performance may differ on your machine, but you will undoubtedly get better performance with ‘in’ parameter.

Now, I am going to do some parallel programming and trying to measure performance gain for in parameter.

Parallel For Loop Without In Parameter

Let’s have a look at the below code snippet which is using “Parallel.For()” but passing parameter without in (passing a new copy each time).

  1. namespace INPerformance  
  2. {     
  3.  class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             BigAstroObject bigAstroObject = new BigAstroObject();  
  8.      bigAstroObject.A001 = 656371600.3042M;  
  9.             bigAstroObject.A002 = 876120136.2188M;  
  10.      ……………………………………………………………………………………………  
  11.      bigAstroObject.A499 = 549329194.3128M;  
  12.             bigAstroObject.A500 = 799448442.2474M;  
  13.      Parallel.For(0, 200_000_000, (int x) => DoSomeThing(bigAstroObject));              
  14.         }  
  15.   
  16.         private static int DoSomeThing(BigAstroObject bigAstroObject)  
  17.         {  
  18.             //your logic  
  19.             return 0;  
  20.         }  
  21.     }  
  22.     struct BigAstroObject  
  23.     {  
  24.         public decimal A001 { get; set; }  
  25.         public decimal A002 { get; set; }  
  26.  ……………………………………………………………………………………  
  27.         public decimal A499 { get; set; }  
  28.         public decimal A500 { get; set; }  
  29.     }  
  30. }  

Refer to the attachment “ParallelForLoopWithoutInParameter.cs” for complete code of the above code snippet.

C# 7.2

Parallel For Loop With In Parameter

Let’s have a look at the below code snippet which is using “Parallel.For()” and passing parameter using in (passing a read-only reference of same copy rather than creating a new copy each time).

  1. namespace INPerformance  
  2. {     
  3.  class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             BigAstroObject bigAstroObject = new BigAstroObject();  
  8.      bigAstroObject.A001 = 656371600.3042M;  
  9.             bigAstroObject.A002 = 876120136.2188M;  
  10.      ……………………………………………………………………………………………  
  11.      bigAstroObject.A499 = 549329194.3128M;  
  12.             bigAstroObject.A500 = 799448442.2474M;  
  13.      Parallel.For(0, 200_000_000, (int x) => DoSomeThing(bigAstroObject));           
  14.         }  
  15.   
  16.         private static int DoSomeThing(in BigAstroObject bigAstroObject)  
  17.         {  
  18.             //your logic  
  19.             return 0;  
  20.         }  
  21.     }  
  22.     struct BigAstroObject  
  23.     {  
  24.         public decimal A001 { get; set; }  
  25.         public decimal A002 { get; set; }  
  26.  ……………………………………………………………………………………  
  27.         public decimal A499 { get; set; }  
  28.         public decimal A500 { get; set; }  
  29.     }  
  30. }  

Refer to the attachment “ParallelForLoopWithInParameter.cs” for complete code of the above code snippet.

C# 7.2

If you would like to explore more about 7.x features, then you can go through following articles.

  1. C# 7.2 New Features with Visual Studio 2017
  2. C# 7.0 And C# 7.1 New Features - Part Two
  3. Top 10 New Features of C# 7 With Visual Studio 2017
  4. Visual Studio 15 Preview First Look & C# 7
  5. Understanding ref and out With C# 7


Similar Articles