Wednesday, December 12, 2018

Quick Introduction to C# Span.

This post is a part of The Second Annual C# Advent. Check out many the other C# related posts there.

Span is a new C# keyword which was introduced in C# version 7.2 on .NET Core 2.1.  C# Span is used in the memory management domain. It can be used to improve memory access performance in many scenarios.

For those of us who also do "Html" development, don't confuse the “Html Span” with the C# Span.  They are completely different in scope and function.

Visual Studio and C# are developed in the open, so we can see what the language designers are considering while adding new features to the C# language.  During the design of the C# 7 new features, Mads Torgersen wrote: 

One feature that could lead to a lot of efficiency would be the ability to have "windows" into arrays - or even onto unmanaged swaths of memory passed along through interop. The amount of copying that could be avoided in some scenarios is probably very significant.

This “window” is what C# Span enables. 

Let's look at a simple case where C# Span can help improve performance.

Example:  C# Substring

This code shows the typical way to get a substring from string using C#.  This is great for getting a portion of a string.

  1. string fullName = "John Jacob Schmidt";
  2. string middleName = fullName.Substring(5, 5);

Now, imagine that not only do we want to get the middle name, but we want to change certain letters in the middleName and have those changes also apply to the fullName.  Without using Span we would have to modify both the middleName string and the fullName string separately.

Span allocates the memory on the stack, the same way that C# Structs are created.  This means the memory is contiguous and can be accessed through the window.

Example:  C# Span slice

Here is similar source code, now using C# Span

  1. Span<char> fullName = "John Jacob Schmidt".ToCharArray().AsSpan();
  2. Span<char> middleName = fullName.Slice(5, 5);
  3. middleName[2] = 'k';
  4. Console.WriteLine(middleName.ToString()); // Prints -> Jakob
  5. Console.WriteLine(fullName.ToString()); // Prints -> John Jakob Schmidt
  6. middleName[6] = 's'// Throws Exception -> System.IndexOutOfRangeException'

On line 1 the Span is allocated.  After that on line 2, a slice is created that encompasses the middleName.  

Line 4, shows changing the third letter of the middleName to the letter 'k'.  Then the middleName and the fullName are both printed.  Notice that the change to the middleName also affected the fullName.

On line 9, we see an access to the seventh character of the middle name.  Since the slice was created to be only 5 characters long, an exception is thrown.  So array bounds are enforced correctly.


This type of memory manipulation was possible before the introduction of C# Span, but the code to do it was very complex and much harder to maintain.  This post shows a simple way to use C# Span.  There are many more uses beyond the simple one shown here.  For further reference look review these articles.

C# Span References:

Code Magazine:
Coding Blast Blog:
MSDN Channel 9:
Mads Torgersen:
MSDN Magazine:

Sunday, January 28, 2018

Recap of .NET Hands on Lab by Phil Japikse

I usually sleep in on Saturday mornings, but on this morning I woke at 6:30, got cleaned up and began my journy to the .NET Core Hands on Lab with Phil Japikse - {Cincinnati}

It was presented by Phil Japikse and hosted by TechElevator (@TechElevator) and Cincinnati DotNet User Group (@cinnug).

The weather was a slight rain, but the 1.5 hour drive was smooth and uneventful.  The workshop started at 9:00 and I arrived at 9:02, just as Phil was introducing himself.  I picked up a doughnut and water and got situated for a day of learning.

Phil began by introducing .NET Core and talking about the pre-reqs for the workshop.  Luckily, I have done a bit of .NET Core 2.0 and tend to keep my Visual Studio up to date, so, I didn't have to install anything in order to get started.

Phil gave us the WIFI info and his workshop GitHub repo.  I downloaded the zip, which included PDFs for all of the labs for the day and a completed workshop project.  I won't be including any links to Phil's repos or information from the workshop, because this course is actually part of a paid educational series.

Several folks were having trouble getting started, but Phil's lab assistant was able to make the rounds and get most of them up and running.  Unfortunately, a couple attendees had to do a full VS download / install.

The first set of labs were about getting our Models and Databases setup.  The PDF's walked us through the process with minimal issues.  Here's a tip:  When cutting and pasting from PDF's, Microsoft Edge tends to remove line returns.  Chrome handles this without an issue.

The day continued on with Phil speaking for about an hour and then labs for a 1/2 hour, back and forth.

Pizza lunch was at 11:30 and was right on time.  It really hit the spot.

I won't go into all of the details of the workshop, but I will say that it far exceeded my expectations.  Before the workshop, my impression of .NET Core was that it was a lesser version of ASP.NET.  Boy, did this misconception get blown away.  Over the course of the workshop, Phil was able to at least introduce every major area of .NET Core.  Wow, it's massive.  I could easily see his training material spread out over a week or 2 week long course.

The one topic, that I was concerned with, going into the lab was deployment with IIS.  It was part of the workshop, but we really didn't have time to focus on it.   I'll have to read through the workshop notes to get a better understanding.

Towards the end of the day, Phil told us that the $5.00 we each donated to Hands Against Hunger would buy around 1000 meals for children in need.  I'm glad that I could help and learn at the same time.

At the end of the workshop, I thanked Phil for presenting and helping us. I said, "this should be a book".  He said, it is a book and showed me the book he co-authored, which covers all of the details in depth.  It's titled:
Building Web Applications with Visual Studio 2017Using .NET Core and Modern JavaScript Frameworks
Here the link: or On Amazon

At 4:00pm I drove back to Columbus and reflected on everything I learned over the day.  When I got home, I took a nap to let my brain catch up with everything I learned that day.  Later, I ordered a copy of the book.

I really enjoyed the .NET Core 2.0 Hands On Lab hosted by TechElevator and Cincinnati DotNet User Group. $5 for breakfast, lunch, hands on lab, presentation and charity donation.  It was time well spent.

Doug Mair