Stackalloc in Nested Expressions in C#

Stackalloc in Nested Expressions in C# 8 with Examples

In this article, I am going to discuss Stackalloc in Nested Expressions in C# 8 with Examples. Please read our previous article where we discussed Unmanaged Constructed Types in C# 8 with Examples. Starting with C# 8.0, if the result of a stackalloc expression is of the System.Span<T> or System.ReadOnlySpan<T> type, you can use the stackalloc expression in other expressions

Stackalloc in Nested Expressions in C#:

The stackalloc operator in C# allocates a block of memory in the stack. The memory block will be created during the execution of the method, and it is automatically deleted when the method is returned. You cannot explicitly free the memory allocated with stackalloc. A stack-allocated memory block is not subject to garbage collection and doesn’t have to be pinned with a fixed statement.

With C# 7, we started using Span<T>, ReadOnlySpan<T>, and Memory<T> because they are ref struct instances that are guaranteed to be allocated on the stack, and therefore won’t affect the garbage collector. Thanks to Span, it was also possible to avoid declaring the stackalloc statements that are directly assigned to Span or ReadOnlySpan as unsafe:

Span<int> nums = stackall

In C# 8.0, if the result of a stackalloc expression is Span<T> or ReadOnlySpan<T>, you can use the stackalloc expression in other expressions. For a better understanding, please have a look at the below example.

using System;
namespace Csharp8Features
{
    public class StackMemoryAllocation
    {
        public static void Main()
        {
            Span<int> numbers = stackalloc[] { 10, 20, 30, 40, 50, 60, 70, 80, 80, 100 };
            var index = numbers.IndexOfAny(stackalloc[] { 40, 60, 100 });
            Console.WriteLine(index);   // output: 3  
        }
    }
}

As you can see the nested stackalloc in the above code. Let’s consider another example.

using System;
namespace Csharp8Features
{
    public class StackMemoryAllocation

    {
        public static void Main()
        {
            Span<int> set = stackalloc[] { 1, 2, 3, 4, 5, 6 };
            var subSet = set.Slice(3, 2);
            foreach (var n in subSet)
            {
                Console.WriteLine(n); // Output: 4 5
            }
        }
    }
}

Starting from C# 8, the compiler widens the use of stackalloc to any expression expecting Span or ReadOnlySpan. In the following example, the test trims the input string from the special characters, obtaining the string specified in the expected variable:

using System;
namespace Csharp8Features
{
    public class StackMemoryAllocation
    {
        public static void Main()
        {
            string input = "C# is a Object Oriented Programming Language \r \n ";
            //string expected = "C# is a Object Oriented Programming Language";
            ReadOnlySpan<char> trimmedChar = input.AsSpan().Trim(stackalloc[] { ' ', '\r', '\n' });

            Console.WriteLine(trimmedChar.ToString());
        }
    }
}

In the next article, I am going to discuss the C# 9 Features with Examples. Here, in this article, I try to explain Stackalloc in Nested Expressions in C# 8 with Examples. I hope you enjoy this Stackalloc in Nested Expressions in C# with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *