Learn how to use in parameters introduced in C# 7.2.
If you liked the post, don't forget to share it and subscribe to the newsletter using the form below the post!
Do you want more? Learn about the Productive C# Membership here.
Transcript Of The Video
In this video I am going to talk about the in parameters feature introduced in C# 7.2.
C# 7.2 added some features that allows to work more efficiently with value types and this is one of those features. I have a console application configure to use the C# 7.2 language and let's have a look at the code that we have and how we can use the in parameters to make the code better.
I have a public struct spaceship that has a name, coordinates and a width and a height. I have a method that takes two spaceships and return a boolean if the two spaceships collide. Then I have a program that create two instances for SpaceShip, the Millennium Falcon and the Enterprise and then I run the collide method 100 millions times and I measure how long does it takes.
Let's try to run it and see what is the result... it's building the solution... now is running and it takes about 2.54 seconds to run. Ok. Perfect. When we call the collide methods, what actually happens is that these two parameters are copied by value. By default C# copy by value value types passed to methods. We all know the in C# there is the ref keyword that can be added to the parameters like this so that instead of passing the parameter by value, you pass it by reference. You need to specify ref when you actually call the method. If I run the code again, you see thta now the performance is a lot better. It is 1.39 seconds. It's basically twice as fast as before. This is something you were always able to do since the beginning of C#.
What C# 7.2 adds is basically the ability to use in as a keyword instead of ref and it means that you still pass the value type by reference but also there is an additional value the is delivering that is it's not possible to change the parameters that actually passed internally. If I do for example, spaceship1.X = 10, you see that the compiler say you cannot assign to a member of variable 'in' SpaceShip because it is a readonly variable. So basically using the in keyword is like using ref readonly. If you use ref there is nothing that garantees the method to actually change the parameters while using the in feature you can have this additional validation from the compiler. If I run this code, you see that he performance is similar to using ref because we are still passing the parameters by reference.
The additional advantage of using in is that you don't need to use in when you actually call the method. You only use in when you actually define the method itself. This is a very small feature but it is quite handy when you deal with value types. It improve performances and at the same time ensure same validation especially when the methods are readonly and don't really need to change the values of the parameters.
The other important thing I want to mention is to always pay attention to measure performance because it is not guarantee that the performance is necessarely better. I believe honestly that the in parameters feature is not only about performance but it is mainly about enforsing the readonly validation. This is one of the biggest value of the in parameter.
The reason why it's called in is because it is complementary to out. This is it, thank you very much for watching.