Tag Archives for " C# 7 "

In Parameters in C# 7.2 – Productive C#

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.

1 Dangerous use of null C# conditional operator in test

Be careful in using the C# null conditional operators in tests

Few days ago, I come across to a bunch of tests that were using the null conditional operator in the wrong way and I think it is worth sharing it.

Have a look at this code and how I fixed it.

Dangerous use of null C# conditional operator in test

What's the problem with the original code?

When result is null, the test pass even if clearly shouldn't!

The reason is because of the "short-circuiting" behaviour.

When result is null, then the rest of the chain’s execution stops! The test then pass because the method ends and by default a test without assertions is a pass.

You can learn more about "short-circuiting"  with null conditional operators here

How did I fix it?

In this particular case I have just removed the null conditional operator and performed a simple comparison using equality instead of relying on calling ToString. 

So...

Be careful when using null conditional operators in assertions and remember about "short-circuiting".



1 Practical C# – How to configure the C# language version in Visual Studio 2017 projects

New video from the Practical C# Video series to learn how to configure the C# language version used.

How to configure your project C# language version

Learn how to configure your C# Visual Studio 2017 (Update 3) project to compile against a specific version of the C# compiler or the latest release.

Do you like these videos? Subscribe here

Previous videos

2 All you need to know to master C# 7

This is a collection of practical videos to learn how to use the new C# 7 features.

Generalized Async Return Types (ValueTask) in C# 7

Learn how to use the generalized async return type feature (more specifically ValueTask) in order to improve the performance of your application by reducing memory pressure. ValueTask is particularly useful when you repeatedly call asynchronous methods that can run synchronously most of the time.

 

Ref returns and locals in C# 7

Learn how you can improve the performance of your application using Ref returns and locals.

 

Local Functions in C# 7

Learn how using local functions in C# 7 can make your code more compact and less repetitive.

 

Binary Literals and Digit Separators

See how to use binary literals and digit separators in C# 7.

 

Is expression with Pattern Matching

See how to make your code more compact using IS expressions with pattern matching instead of the AS keyword.

 

Switch Statement with Pattern Matching

Learn how to refactor a series of ifs using the improved switch statement with pattern matching.

 

Throw Expressions

Learn how to use the throw expressions to make your validation code more compact and easy to read.

 

Inline Out Variables 

Learn how to inline out variables and how to use Visual Studio 2017 to inline all variable declarations in the entire solution.

 

Refactor out parameters into tuple

Learn how to refactor a method that return multiple values using out parameters to use C# 7 Tuples instead.

 

Tuple Semantic Names

Understand how tuples works under the cover and why returning tuples in public APIs is probably not a good idea.

 

Tuple Deconstructions

How to deconstruct a tuple into its individual components.

 

Expression bodied members

Learn how you can make your class extremely more compact using expression bodied members.

 

Want to become a master C# developer?

All you need to do is to subscribe here.

 

2 ValueTask in C# – Generalized Async Return Types

New video from the Practical C# Video series. Explore C# in a simple and practical way.

Generalized Async Return Types (ValueTask)

Learn how to use the generalized async return type feature in C# 7 (more specifically ValueTask) in order to improve the performance of you application by reducing memory pressure. ValueTask is particularly useful when you repeatedly call asynchronous methods that can run synchronously most of the time.

 

Do you like these videos? Subscribe here

Previous videos

>