23

Everyone is writing about how great the new type Span<T> is so I eagerly wanted to start rewriting a couple of methods in my libraries but where do I actually find it? I've updated Visual Studio 2017 to the latest version 15.5.0 where the change-log says:

The C# compiler now supports the 7.2 set of language features including:

  • Support for the Span<T> type being used throughout Kestrel and CoreFX via the ref struct modifier.

but when I try to use it my code I'm getting an error and intellisense cannot resolve it. It's a .net 4.6.2 project and the language version is set to latest minor.

Do I need to install some NuGet package in order to be able to use it? I can't figure this out.

Community
  • 1
  • 1
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • 6
    It's confusing indeed. This article https://msdn.microsoft.com/en-us/magazine/mt814808.aspx describes Span as if it were part of the framework: `System.Span is a new value type at the heart of .NET.`, only to mention at the very end in the `What's Next` section the need to install the `System.Memory` NuGet package. – user276648 Jul 18 '18 at 02:55
  • @user276648 now that the package is released, you can at least find it in the main nuget-feed very easily. At the time I was asking this question it could be found only in some weird & alternative feed for core-fx and it wasn't easy to figure this one out :-| – t3chb0t Jul 18 '18 at 14:48

2 Answers2

21

You need to install prerelease version (check "Include prerelease" checkbox in nuget manager) of System.Memory package. Then just use Span (it's in System namespace).

Evk
  • 98,527
  • 8
  • 141
  • 191
  • 8
    Why do they release a compiler that supports someting that is still beta? No wonder this is so confusing :-| so this actually means _Hey!, our compiler now supports a new type `Span` but you cannot use it because we hadn't actually released it yet_ – t3chb0t Dec 05 '17 at 09:58
  • @t3chb0t well it's not exactly related to compiler. This package is already compiled, so is not related to compiler version you have on your machine. But new version of compiler provides features which are useful for this type usecase (but those features are useful even without that type of course). – Evk Dec 05 '17 at 10:02
  • Ok, but then I don't understand why the change-log mentions the compiler? If this new feature can be used without dedicated compiler why bother? Couldn't they just publish the package? Everyone would be happy to read _here's the package with the new `Span`_ :-( I thought it would be comming with this update but apparently it didn't. – t3chb0t Dec 05 '17 at 10:06
  • 1
    @t3chb0t Quote you mention in question says "Support for the Span type being used throughout Kestrel and CoreFX via **the ref struct modifier**" - the bold thing is C# 7.2 compiler feature, not Span class itself. As for why this package is in beta still - well, I have no idea. – Evk Dec 05 '17 at 10:12
  • 1
    This is confusing as hell. Documentation on these new ref locals / ref returns / Span features is very barebones as well. – JBeurer Jan 01 '19 at 21:12
8

Visual Studio 2019: If you are using the full .NET Framework (e.g. 4.7.2):

  1. Go to NuGet packagemanager
  2. Search for System.Memory package by Microsoft and install it.

Now you will be able to use Span<T>.

No need to search for pre-release versions anymore.

juFo
  • 17,849
  • 10
  • 105
  • 142