1

In a normal comparison of types typeof(nint)==typeof(System.IntPtr) is true

so I was expecting that the SpecialType of System.IntPtr is nint, just like System.Int32 and int, when doing CodeAnalysis using Microsoft.CodeAnalysis.

but it turns out the SpecialType of System.IntPtr is still System.IntPtr.

here's my code

string GetSpecialTypeForDisplayIfAny(ISymbol symbol)
{
   return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat
            .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)
            .AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)
            .AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.UseSpecialTypes)
            .AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)
            .AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNotNullableReferenceTypeModifier))
);
}

So my question is why is the SpecialType of System.IntPtr not nint in CodeAnalysis?

PS. I've already gone through this documentation. I understand that ".. the keywords are not simply aliases for the types." but why is this true then typeof(nint)==typeof(System.IntPtr)?

Marlon Dumal-is
  • 300
  • 2
  • 13
  • 3
    `nint` is a C# v9 keyword that maps to IntPtr, the type that the runtime understands. Just like `int` maps to Int32 and `string` to String, etc. It is indeed not a simple alias like int, they removed the dangerous members like ToInt32(), ToPointer(), etc. Having code analysis represent it in SpecialType is sensible, except that it takes an act of God to modify [the core types](https://github.com/dotnet/roslyn/blob/main/docs/compilers/Co-located%20core%20types.md). You can ask Him at github, but odds are not good. – Hans Passant May 20 '21 at 11:41
  • That's just sad, so I guess the best I can do now is to just manually replace 'System.IntPtr' and 'System.UIntPtr' using 'Replace("System.IntPtr", "nint").Replace("System.UIntPtr", "nuint")'? – Marlon Dumal-is May 20 '21 at 12:19

0 Answers0