November 20

Extensible Fixed Statement in c# 7.3


Extensible fixed statement is a new feature introduced in C# 7.3. Learn how to add support for pinning using the fixed statement on your own types

Transcript of the video

Hi! I'm Andrea, and welcome to Productive C#. In this video, I want to talk about the extensible fixed statement feature introduced in C# 7.3. Let me run this console application, so that I can show you what it does. This application is printing a pattern of colours, as a console application. I have a class picture that contains a read-only collection of colours.

Then I have height and width property. I neutralise the colours in the construction of the picture. I have the ability to select a particular row. Then I have the ability to print basically what is stored in the colours buffer. Basically, the colour buffer contains the colours of the entire window and then print actually uses the console foreground colour property, set the colour, and then print it on the screen. I will get in a second on this matter here.

Let's have a look at the programme main, so I'm using unsafe here, and the reason is because I'm using the fixed statement, so what I do is I grab a picture, then for each row I call the DrawRow method and then I print the picture. The DrawRow method used the fixed statement, to get a pointer to a particular row inside the picture, and specifically is getting a pointer to a location, where the selected row starts. This means that using a fixed statement I can pretty much do anything I want on that location of memory using pointers, as you can see here I am using pointer as well here I am trying to access the elements of a point, in this particularly map them, what I'm doing in this side here, in this particular code, what really matters is that I'm using a fixed statement, using a particular type.

This is not possible before C# 7.3 because you can use the fixed statement on arrays, on strings, fixed side buffers, or using any unmanaged variables like primitive types for example, or enumerators. Here instead I'm using particular type that I defined inside a fixed statement, and this is basically were these features can be seen. It allows you to by defining a simple method called GetPinnableReference gives you the ability to use any type inside A fixed statement, this method must have a specific signature, it must return our ref type, to an unmanaged type, so as you can see here this method is returning a reference to a ConsoleColor, ConsoleColor is a enumerator, is an unmanaged type, so you can do that, and basically what I'm doing here inside the code, I'm returning a references to the location in colours, when the rows start. This is the selected.

Okay, so this method here enables to use a fixed statement using the picture. If I remove this code here, you will get a compiler error, as you can see and saying that the given expression can not be used in a fixed statement. So it is very important in order to be able to use any type, in a fixed statement to define this method here. This is obviously a very simple example just for demonstration purposes, it's very unlikely that you are going to need to define your own GetPinnableReference and its mostly useful when you want to return a reference to unmnaged data or like data in this case I'm just returning a reference to an arrays of colours, but it's important to know that this array was read only, but it doesn't really matter because I'm returning a pointer, and I'm doing all the operations, so it doesn't really matter that this is read only because we have safe context when we use it here instead of fixed context.

The interesting thing is that some types in the framework like Span<T> this has been introduced with inter core 2.0, now supports pinning using fixed statements and then you can use Span inside a fixed statement. If you go to Github you can see an example of the implementation, of GetPinnableReference in span, I don't get into the details of this because it's quite complicated under the covers, and is really not that particularly useful for you to know. The most important things for you is to know that Span<T> supports pinning using a fixed statement, and other types in the future might also support it and if you want you can also add the support yourself in your own types, and this is pretty much what this feature is about. It allows you to provide fixed statement support for any type, as far as you implement the GetPinnableReference method, and that's it.

Thank you very much for watching.


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!

Get instant access to the Master C# 9 webinar to learn all the new exciting C# 9 features quickly.
Get regular videos and news on C# and .NET and offers on products and services to master C#.

We will collect, use and protect your data in accordance with our Privacy Policy.