Home > other >  The name 'u8' does not exist in the current context in ASP.NET Core Microsoft.Net.Http.Hea
The name 'u8' does not exist in the current context in ASP.NET Core Microsoft.Net.Http.Hea

Time:05-03

I try to build ASP.NET Core source code for Microsoft.Net.Http.Headers but got lots of errors due to some code like below. How can I fix it please? The IDE that I am using is Visual Studio 2022 Community. The language version shows preview.

private static ReadOnlySpan<byte> MimePrefix => "\"=?utf-8?B?"u8;

enter image description here

CodePudding user response:

After reading Encoded-word Syntax, I still don't know what you want, so please just tell me what kind of string you need and I will help you further.

You can refer to the following code first:

private static string MimePrefix => "\"=?utf-8?B?\"u8"; // '=?utf-8?B?'u8
private static ReadOnlySpan<byte> MimePrefix1 => Encoding.ASCII.GetBytes("=?utf-8?B?");

CodePudding user response:

It's a new feature of C# 11 - Utf8 Strings Literals:

string s1 = "hello"u8;             // Error
var s2 = "hello"u8;                // Okay and type is byte[]
Span<byte> s3 = "hello"u8;         // Okay due to an implicit user-defined conversion from byte[] declared on Span<byte>.
ReadOnlySpan<byte> s3 = "hello"u8; // Okay due to an implicit user-defined conversion from byte[] declared on ReadOnlySpan<byte>.

and the feature is merged into Visual Studio 17.3p1.

Just like the commit's author talk:

Also, folks likely need to update VS for the feature to light up.

  • Related