-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Extended nameof scope works in C# 8?
Please refer to the following output of the code snippet (provided below the output) running on Visual Studio 2022 Community Edition for Apple Silicon, MacBook Pro M1 Pro, macOS Ventura Version 13.5.2 (22G91).
NB:
The code snippet comments include compiler errors for features greater than C# 8 (C# 9, C# 10, and C# 11) only to find out if "extended nameof scope" works in C# 8?
Output
/**********************************************************************************************************************/
OS Information:
Platform: Unix
Version String: Unix 22.6.0.0
Version Information:
Major: 22
Minor: 6
Service Pack: ''
Environment.Version: 3.1.28
RuntimeInformation.FrameworkDescription: .NET Core 3.1.28
Extended nameof scope
nameof expression with a method parameter
/**********************************************************************************************************************/
Code Snippet
/**********************************************************************************************************************/
// <Nullable>enable</Nullable>
class NameOfExpression
{
// Works in CS 8?
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(url))]
string? GetTopLevelDomainFromFullUrl(string? url) => url;
// Works in CS 8?
[ParameterString(nameof(msg))]
public void Method(string msg)
{
// Error CS8400: Feature 'local function attributes' is not available in C# 8.0. Please use language version 9.0 or greater.
// LocalFunction<string>(msg);
// [ParameterString(nameof(T))]
// void LocalFunction<T>(T param)
// {
// System.Console.WriteLine(param);
// }
System.Console.WriteLine(msg);
// Error CS8400: Feature 'lambda attributes' is not available in C# 8.0. Please use language version 10.0 or greater.
// Error CS8400: Feature 'inferred delegate type' is not available in C# 8.0. Please use language version 10.0 or greater.
// var lambdaExpression = ([ParameterString(nameof(aNumber))] int aNumber) => aNumber.ToString();
}
}
// Custom Attribute
class ParameterStringAttribute : System.Attribute
{
public ParameterStringAttribute(string msg) { }
}
class PatternMatchSpan
{
public void Print()
{
// System.Console.WriteLine("Pattern match Span<char> or ReadOnlySpan<char> on a constant string: Span<char>");
string contentLength = "Content-Length: 132";
System.Span<char> span = contentLength.ToCharArray();
System.Span<char> slice = span.Slice(16);
// Error CS8400: Feature 'pattern matching ReadOnly/Span<char> on constant string' is not available in C# 8.0. Please use language version 11.0 or greater.
// System.Console.WriteLine($"Pattern match Span<char>: {slice is "132"}");
// System.Console.WriteLine("Pattern match Span<char> or ReadOnlySpan<char> on a constant string: ReadOnlySpan<char>");
System.ReadOnlySpan<char> spanReadOnly = contentLength.ToCharArray();
System.ReadOnlySpan<char> sliceReadOnly = spanReadOnly.Slice(16);
// Error CS8400: Feature 'pattern matching ReadOnly/Span<char> on constant string' is not available in C# 8.0. Please use language version 11.0 or greater.
// System.Console.WriteLine($"Pattern match ReadOnlySpan<char>: {sliceReadOnly is "132"}");
}
}
class Program
{
static void Main(string[] args)
{
// Version
/******************************************************************************/
var os = System.Environment.OSVersion;
System.Console.WriteLine("OS Information:");
System.Console.WriteLine("Platform: {0:G}", os.Platform);
System.Console.WriteLine("Version String: {0}", os.VersionString);
System.Console.WriteLine("Version Information:");
System.Console.WriteLine(" Major: {0}", os.Version.Major);
System.Console.WriteLine(" Minor: {0}", os.Version.Minor);
System.Console.WriteLine("Service Pack: '{0}'", os.ServicePack);
// Environment.Version property returns the .NET runtime version for .NET 5+ and .NET Core 3.x
// Not recommend for .NET Framework 4.5+
System.Console.WriteLine($"Environment.Version: {System.Environment.Version}");
// RuntimeInformation.FrameworkDescription property gets the name of the .NET installation on which an app is running
// .NET 5+ and .NET Core 3.x // .NET Framework 4.7.1+ // Mono 5.10.1+
System.Console.WriteLine($"RuntimeInformation.FrameworkDescription: {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}");
System.Console.WriteLine();
/******************************************************************************/
System.Console.WriteLine("Extended nameof scope");
NameOfExpression expressionNameOf = new NameOfExpression();
expressionNameOf.Method("nameof expression with a method parameter");
System.Console.WriteLine();
// Error CS8400: Feature 'target-typed object creation' is not available in C# 8.0. Please use language version 9.0 or greater.
// PatternMatchSpan spanPatternMatch = new();
PatternMatchSpan spanPatternMatch = new PatternMatchSpan();
// System.Console.WriteLine("Pattern match Span<char> or ReadOnlySpan<char> on a constant string");
spanPatternMatch.Print();
}
}
/**********************************************************************************************************************/