VS2017运行C#7.2 Span功能的.NET2.1 SDK获取及CS0305问题咨询
解决.NET Core 2.1 SDK获取与Span代码错误问题
Hey there, let's work through your issues step by step:
一、获取.NET Core 2.1 SDK并在VS2017中启用
First off, .NET Core 2.1 isn't included by default in all VS2017 installations. Here's how to get it set up:
- Open Visual Studio 2017, go to the Tools menu, then select Get Tools and Features. This launches the VS Installer.
- In the installer, locate the .NET Core cross-platform development workload. Check if .NET Core 2.1 SDK is listed under its components—if not, tick the corresponding option and click Modify to install it.
- If you can't find it in the VS Installer, go to Microsoft's official .NET Core archive download area to grab the .NET Core 2.1 SDK installer directly. Run the installer, restart VS2017, and you'll be able to select .NET Core 2.1 as your project's target framework.
- Also, ensure your VS2017 is updated to version 15.3 or later—this is the minimum version required to support .NET Core 2.1.
二、修复代码中的CS0305错误
The error you're hitting comes from a couple of small code issues:
- You don't need
using System.Memory;—bothSpan<T>andMemory<T>live in theSystemnamespace in .NET Core 2.1, so just keepusing System;. - The CS0305 error means you tried to use the generic
Memorytype without specifying its type parameter. While your posted code doesn't show this, double-check the omitted...section of your code to make sure you aren't usingMemorysomewhere without adding a type (likeMemory<byte>instead of justMemory).
Here's the corrected version of your code:
using System; namespace sim { class Program { static void Main(string[] args) { var arr = new byte[10]; Span<byte> bytes = arr; // Implicit cast from T[] to Span<T> Span<byte> slicedBytes = bytes.Slice(start: 5, length: 2); } } }
A critical reminder: Make sure your project is targeting .NET Core 2.1, not the traditional .NET Framework (like 2.0). Span
内容的提问来源于stack exchange,提问作者Mdev
相关产品推荐
相关产品推荐

