Build with an Azure free account. Get USD200 credit for 30 days and 12 months of free services.

Start free today

C# Language Highlights: The "in" Parameter Modifier

Play C# Language Highlights: The "in" Parameter Modifier

The Discussion

  • User profile image
    Why didnt they reuse the readonly keyword , would have been suited better in that situation ? Would have made the code more readable .
  • User profile image

  • User profile image

    Sorry about the earlier message, bad case of mobile copy/pasting.

    Anyway, unless I'm mistaken, that video completely misses the point of that keyword, as it was not created for the purpose of preventing the reuse of a parameter variable within a method (otherwise people are going to think they should put it nearly everywhere).
    If you remove the 'in' keyword as you did in the video, it doesn't change anything for the caller. Reassigning the 'input' parameter within the Print() method doesn't change the values held by the 'person' or 'number' variables in Main().
    If you were using the 'ref' keyword though, it would. But if you didn't want your variables to be affected, then using 'ref' with a reference type such as Object wouldn't make sense! It would instead make sense to use 'ref' with value types (struct) for performance reasons, to avoid the copying of data that passing by value incurs. And that's where 'in' is useful, because you can now get that performance benefit of 'ref' without risking the called method changing your variables.
    So basically for your example to have made sense, you should have changed the type of 'input' to a value type such as int, or made the Print() method generic with a constraint of struct for the type parameter (so bye bye record). And you should at least have mentioned that the main purpose of that keyword was for performance with value types.

  • User profile image

    the cat is cute because

Add Your 2 Cents