src/Compilers/CSharp/Test/Semantic/Semantics/NativeIntegerTests.cs
/// <summary>
/// Instance members ToInt32(), ToInt64(), ToPointer() are explicitly excluded from nint and nuint.
/// Other instance members are implicitly included on nint and nuint.
/// </summary>
Instance members ToInt32(), ToInt64(), ToPointer() are explicitly excluded from nint and nuint.
Other instance members are implicitly included on nint and nuint.
Instance members ToInt32 () ToInt64 () ToPointer () are explicitly excluded from nint and nuint . Other instance members are implicitly included on nint and nuint .
[Theory]
        [InlineData(false)]
        [InlineData(true)]
        public void InstanceMembers(bool useCompilationReference)
        {
            var sourceA =
@"namespace System
{
    public class Object { }
    public class String { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
    public struct Int64 { }
    public struct UInt32 { }
    public struct UInt64 { }
    public interface IFormatProvider { }
    public struct IntPtr
    {
        public int ToInt32() => default;
        public long ToInt64() => default;
        public uint ToUInt32() => default;
        public ulong ToUInt64() => default;
        unsafe public void* ToPointer() => default;
        public int CompareTo(object other) => default;
        public int CompareTo(IntPtr other) => default;
        public bool Equals(IntPtr other) => default;
        public string ToString(string format) => default;
        public string ToString(IFormatProvider provider) => default;
        public string ToString(string format, IFormatProvider provider) => default;
    }
    public struct UIntPtr
    {
        public int ToInt32() => default;
        public long ToInt64() => default;
        public uint ToUInt32() => default;
        public ulong ToUInt64() => default;
        unsafe public void* ToPointer() => default;
        public int CompareTo(object other) => default;
        public int CompareTo(UIntPtr other) => default;
        public bool Equals(UIntPtr other) => default;
        public string ToString(string format) => default;
        public string ToString(IFormatProvider provider) => default;
        public string ToString(string format, IFormatProvider provider) => default;
    }
}";
            var comp = CreateEmptyCompilation(sourceA, options: TestOptions.UnsafeReleaseDll);
            comp.VerifyDiagnostics();
            var refA = AsReference(comp, useCompilationReference);
            var sourceB =
@"using System;
class Program
{
    unsafe static void F1(nint i)
    {
        _ = i.ToInt32();
        _ = i.ToInt64();
        _ = i.ToUInt32();
        _ = i.ToUInt64();
        _ = i.ToPointer();
        _ = i.CompareTo(null);
        _ = i.CompareTo(i);
        _ = i.Equals(i);
        _ = i.ToString((string)null);
        _ = i.ToString((IFormatProvider)null);
        _ = i.ToString((string)null, (IFormatProvider)null);
    }
    unsafe static void F2(nuint u)
    {
        _ = u.ToInt32();
        _ = u.ToInt64();
        _ = u.ToUInt32();
        _ = u.ToUInt64();
        _ = u.ToPointer();
        _ = u.CompareTo(null);
        _ = u.CompareTo(u);
        _ = u.Equals(u);
        _ = u.ToString((string)null);
        _ = u.ToString((IFormatProvider)null);
        _ = u.ToString((string)null, (IFormatProvider)null);
    }
}";
            comp = CreateEmptyCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview, options: TestOptions.UnsafeReleaseDll);
            comp.VerifyDiagnostics(
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToInt32").WithArguments("nint", "ToInt32").WithLocation(6, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToInt64").WithArguments("nint", "ToInt64").WithLocation(7, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToUInt32").WithArguments("nint", "ToUInt32").WithLocation(8, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToUInt64").WithArguments("nint", "ToUInt64").WithLocation(9, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToPointer").WithArguments("nint", "ToPointer").WithLocation(10, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToInt32").WithArguments("nuint", "ToInt32").WithLocation(20, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToInt64").WithArguments("nuint", "ToInt64").WithLocation(21, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToUInt32").WithArguments("nuint", "ToUInt32").WithLocation(22, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToUInt64").WithArguments("nuint", "ToUInt64").WithLocation(23, 15),
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "ToPointer").WithArguments("nuint", "ToPointer").WithLocation(24, 15));
            verifyType((NamedTypeSymbol)comp.GetMember<MethodSymbol>("Program.F1").Parameters[0].Type, signed: true);
            verifyType((NamedTypeSymbol)comp.GetMember<MethodSymbol>("Program.F2").Parameters[0].Type, signed: false);
            static void verifyType(NamedTypeSymbol type, bool signed)
            {
                Assert.True(type.IsNativeIntegerType);
                VerifyType(type, signed: signed, isNativeInt: true);
                VerifyType(type.GetPublicSymbol(), signed: signed, isNativeInt: true);
                var members = type.GetMembers().Sort(SymbolComparison);
                var actualMembers = members.SelectAsArray(m => m.ToTestDisplayString());
                var expectedMembers = new[]
                {
                    $"System.Boolean {type}.Equals({type} other)",
                    $"System.Int32 {type}.CompareTo(System.Object other)",
                    $"System.Int32 {type}.CompareTo({type} other)",
                    $"System.String {type}.ToString(System.IFormatProvider provider)",
                    $"System.String {type}.ToString(System.String format)",
                    $"System.String {type}.ToString(System.String format, System.IFormatProvider provider)",
                    $"{type}..ctor()",
                };
                AssertEx.Equal(expectedMembers, actualMembers);
                var underlyingType = type.NativeIntegerUnderlyingType;
                VerifyMembers(underlyingType, type, signed);
                VerifyMembers(underlyingType.GetPublicSymbol(), type.GetPublicSymbol(), signed);
            }
        }
[ Theory ] [ InlineData ( false ) ] [ InlineData ( true ) ] public void InstanceMembers ( bool useCompilationReference ) { var sourceA = @"namespace System
{
    public class Object { }
    public class String { }
    public abstract class ValueType { }
    public struct Void { }
    public struct Boolean { }
    public struct Int32 { }
    public struct Int64 { }
    public struct UInt32 { }
    public struct UInt64 { }
    public interface IFormatProvider { }
    public struct IntPtr
    {
        public int ToInt32() => default;
        public long ToInt64() => default;
        public uint ToUInt32() => default;
        public ulong ToUInt64() => default;
        unsafe public void* ToPointer() => default;
        public int CompareTo(object other) => default;
        public int CompareTo(IntPtr other) => default;
        public bool Equals(IntPtr other) => default;
        public string ToString(string format) => default;
        public string ToString(IFormatProvider provider) => default;
        public string ToString(string format, IFormatProvider provider) => default;
    }
    public struct UIntPtr
    {
        public int ToInt32() => default;
        public long ToInt64() => default;
        public uint ToUInt32() => default;
        public ulong ToUInt64() => default;
        unsafe public void* ToPointer() => default;
        public int CompareTo(object other) => default;
        public int CompareTo(UIntPtr other) => default;
        public bool Equals(UIntPtr other) => default;
        public string ToString(string format) => default;
        public string ToString(IFormatProvider provider) => default;
        public string ToString(string format, IFormatProvider provider) => default;
    }
}" ; var comp = CreateEmptyCompilation ( sourceA , options : TestOptions . UnsafeReleaseDll ) ; comp . VerifyDiagnostics ( ) ; var refA = AsReference ( comp , useCompilationReference ) ; var sourceB = @"using System;
class Program
{
    unsafe static void F1(nint i)
    {
        _ = i.ToInt32();
        _ = i.ToInt64();
        _ = i.ToUInt32();
        _ = i.ToUInt64();
        _ = i.ToPointer();
        _ = i.CompareTo(null);
        _ = i.CompareTo(i);
        _ = i.Equals(i);
        _ = i.ToString((string)null);
        _ = i.ToString((IFormatProvider)null);
        _ = i.ToString((string)null, (IFormatProvider)null);
    }
    unsafe static void F2(nuint u)
    {
        _ = u.ToInt32();
        _ = u.ToInt64();
        _ = u.ToUInt32();
        _ = u.ToUInt64();
        _ = u.ToPointer();
        _ = u.CompareTo(null);
        _ = u.CompareTo(u);
        _ = u.Equals(u);
        _ = u.ToString((string)null);
        _ = u.ToString((IFormatProvider)null);
        _ = u.ToString((string)null, (IFormatProvider)null);
    }
}" ; comp = CreateEmptyCompilation ( sourceB , references : new [ ] { refA } , parseOptions : TestOptions . RegularPreview , options : TestOptions . UnsafeReleaseDll ) ; comp . VerifyDiagnostics ( Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 6 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 7 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 8 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 9 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 10 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 20 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 21 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 22 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 23 , 15 ) , Diagnostic ( ErrorCode . ERR_NoSuchMemberOrExtension , " " ) . WithArguments ( " " , " " ) . WithLocation ( 24 , 15 ) ) ; verifyType ( ( NamedTypeSymbol ) comp . GetMember < MethodSymbol > ( " " ) . Parameters [ 0 ] . Type , signed : true ) ; verifyType ( ( NamedTypeSymbol ) comp . GetMember < MethodSymbol > ( " " ) . Parameters [ 0 ] . Type , signed : false ) ; static void verifyType ( NamedTypeSymbol type , bool signed ) { Assert . True ( type . IsNativeIntegerType ) ; VerifyType ( type , signed : signed , isNativeInt : true ) ; VerifyType ( type . GetPublicSymbol ( ) , signed : signed , isNativeInt : true ) ; var members = type . GetMembers ( ) . Sort ( SymbolComparison ) ; var actualMembers = members . SelectAsArray ( m => m . ToTestDisplayString ( ) ) ; var expectedMembers = new [ ] { $" System.Boolean  { type } .Equals( { type }  other) " , $" System.Int32  { type } .CompareTo(System.Object other) " , $" System.Int32  { type } .CompareTo( { type }  other) " , $" System.String  { type } .ToString(System.IFormatProvider provider) " , $" System.String  { type } .ToString(System.String format) " , $" System.String  { type } .ToString(System.String format, System.IFormatProvider provider) " , $" { type } ..ctor() " , } ; AssertEx . Equal ( expectedMembers , actualMembers ) ; var underlyingType = type . NativeIntegerUnderlyingType ; VerifyMembers ( underlyingType , type , signed ) ; VerifyMembers ( underlyingType . GetPublicSymbol ( ) , type . GetPublicSymbol ( ) , signed ) ; } }
Instance members ToInt32(), ToInt64(), ToPointer() are explicitly excluded from nint and nuint.
Instance members ToInt32 () ToInt64 () ToPointer () are explicitly excluded from nint and nuint .
// (6,15): error CS1061: 'nint' does not contain a definition for 'ToInt32' and no accessible extension method 'ToInt32' accepting a first argument of type 'nint' could be found (are you missing a using directive or an assembly reference?) //         _ = i.ToInt32(); // (7,15): error CS1061: 'nint' does not contain a definition for 'ToInt64' and no accessible extension method 'ToInt64' accepting a first argument of type 'nint' could be found (are you missing a using directive or an assembly reference?) //         _ = i.ToInt64(); // (8,15): error CS1061: 'nint' does not contain a definition for 'ToUInt32' and no accessible extension method 'ToUInt32' accepting a first argument of type 'nint' could be found (are you missing a using directive or an assembly reference?) //         _ = i.ToUInt32(); // (9,15): error CS1061: 'nint' does not contain a definition for 'ToUInt64' and no accessible extension method 'ToUInt64' accepting a first argument of type 'nint' could be found (are you missing a using directive or an assembly reference?) //         _ = i.ToUInt64(); // (10,15): error CS1061: 'nint' does not contain a definition for 'ToPointer' and no accessible extension method 'ToPointer' accepting a first argument of type 'nint' could be found (are you missing a using directive or an assembly reference?) //         _ = i.ToPointer(); // (20,15): error CS1061: 'nuint' does not contain a definition for 'ToInt32' and no accessible extension method 'ToInt32' accepting a first argument of type 'nuint' could be found (are you missing a using directive or an assembly reference?) //         _ = u.ToInt32(); // (21,15): error CS1061: 'nuint' does not contain a definition for 'ToInt64' and no accessible extension method 'ToInt64' accepting a first argument of type 'nuint' could be found (are you missing a using directive or an assembly reference?) //         _ = u.ToInt64(); // (22,15): error CS1061: 'nuint' does not contain a definition for 'ToUInt32' and no accessible extension method 'ToUInt32' accepting a first argument of type 'nuint' could be found (are you missing a using directive or an assembly reference?) //         _ = u.ToUInt32(); // (23,15): error CS1061: 'nuint' does not contain a definition for 'ToUInt64' and no accessible extension method 'ToUInt64' accepting a first argument of type 'nuint' could be found (are you missing a using directive or an assembly reference?) //         _ = u.ToUInt64(); // (24,15): error CS1061: 'nuint' does not contain a definition for 'ToPointer' and no accessible extension method 'ToPointer' accepting a first argument of type 'nuint' could be found (are you missing a using directive or an assembly reference?) //         _ = u.ToPointer();
useCompilationReference
bool