Números inteiros nativos nint
e nuint
simples, escritos em IL, provavelmente o segundo melhor em relação ao suporte adequado do compilador. Totalmente funcional.
DotNetCross.NativeInts
https://www.nuget.org/packages/DotNetCross.NativeInts
nint
public struct nint : IEquatable < nint > , IComparable < nint >
{
public static readonly nint Zero ;
public IntPtr Value ;
public nint ( IntPtr value ) ;
public nint ( int value ) ;
public nint ( long value ) ;
public int CompareTo ( nint other ) ;
public override bool Equals ( object obj ) ;
public bool Equals ( nint other ) ;
public override int GetHashCode ( ) ;
public override string ToString ( ) ;
public static nint operator + ( nint value ) ;
public static nint operator + ( nint l , nint r ) ;
public static nint operator + ( nint l , int r ) ;
public static nint operator + ( int l , nint r ) ;
public static nint operator - ( nint value ) ;
public static nint operator - ( nint l , int r ) ;
public static nint operator - ( nint l , nint r ) ;
public static nint operator - ( int l , nint r ) ;
public static nint operator ~ ( nint value ) ;
public static nint operator ++ ( nint value ) ;
public static nint operator -- ( nint value ) ;
public static nint operator * ( nint l , nint r ) ;
public static nint operator * ( nint l , int r ) ;
public static nint operator / ( nint l , int r ) ;
public static nint operator / ( nint l , nint r ) ;
public static nint operator % ( nint l , nint r ) ;
public static nint operator % ( nint l , int r ) ;
public static nint operator & ( nint l , int r ) ;
public static nint operator & ( nint l , nint r ) ;
public static nint operator | ( nint l , nint r ) ;
public static nint operator | ( nint l , int r ) ;
public static nint operator ^ ( nint l , nint r ) ;
public static nint operator ^ ( nint l , int r ) ;
public static nint operator << ( nint l , nint r ) ;
public static nint operator << ( nint l , int r ) ;
public static nint operator >> ( nint l , nint r ) ;
public static nint operator >> ( nint l , int r ) ;
public static bool operator == ( IntPtr l , nint r ) ;
public static bool operator == ( nint l , IntPtr r ) ;
public static bool operator == ( nint l , long r ) ;
public static bool operator == ( long l , nint r ) ;
public static bool operator == ( nint l , int r ) ;
public static bool operator == ( nint l , nint r ) ;
public static bool operator == ( int l , nint r ) ;
public static bool operator != ( IntPtr l , nint r ) ;
public static bool operator != ( long l , nint r ) ;
public static bool operator != ( nint l , IntPtr r ) ;
public static bool operator != ( nint l , long r ) ;
public static bool operator != ( int l , nint r ) ;
public static bool operator != ( nint l , int r ) ;
public static bool operator != ( nint l , nint r ) ;
public static bool operator < ( IntPtr l , nint r ) ;
public static bool operator < ( nint l , IntPtr r ) ;
public static bool operator < ( long l , nint r ) ;
public static bool operator < ( nint l , long r ) ;
public static bool operator < ( int l , nint r ) ;
public static bool operator < ( nint l , nint r ) ;
public static bool operator < ( nint l , int r ) ;
public static bool operator > ( IntPtr l , nint r ) ;
public static bool operator > ( long l , nint r ) ;
public static bool operator > ( nint l , long r ) ;
public static bool operator > ( int l , nint r ) ;
public static bool operator > ( nint l , int r ) ;
public static bool operator > ( nint l , nint r ) ;
public static bool operator > ( nint l , IntPtr r ) ;
public static bool operator <= ( nint l , int r ) ;
public static bool operator <= ( IntPtr l , nint r ) ;
public static bool operator <= ( nint l , IntPtr r ) ;
public static bool operator <= ( long l , nint r ) ;
public static bool operator <= ( nint l , long r ) ;
public static bool operator <= ( int l , nint r ) ;
public static bool operator <= ( nint l , nint r ) ;
public static bool operator >= ( nint l , nint r ) ;
public static bool operator >= ( nint l , IntPtr r ) ;
public static bool operator >= ( long l , nint r ) ;
public static bool operator >= ( nint l , long r ) ;
public static bool operator >= ( int l , nint r ) ;
public static bool operator >= ( nint l , int r ) ;
public static bool operator >= ( IntPtr l , nint r ) ;
public static implicit operator long ( nint value ) ;
public static implicit operator IntPtr ( nint value ) ;
public static implicit operator nint ( int value ) ;
public static implicit operator nint ( IntPtr value ) ;
public static explicit operator nint ( long value ) ;
public static explicit operator int ( nint value ) ;
}
nuint
public struct nuint : IEquatable < nuint > , IComparable < nuint >
{
public static readonly nuint Zero ;
public UIntPtr Value ;
public nuint ( UIntPtr value ) ;
public nuint ( uint value ) ;
public nuint ( ulong value ) ;
public int CompareTo ( nuint other ) ;
public override bool Equals ( object obj ) ;
public bool Equals ( nuint other ) ;
public override int GetHashCode ( ) ;
public override string ToString ( ) ;
public static nuint operator + ( nuint value ) ;
public static nuint operator + ( nuint l , nuint r ) ;
public static nuint operator + ( nuint l , uint r ) ;
public static nuint operator + ( uint l , nuint r ) ;
public static nuint operator - ( nuint l , uint r ) ;
public static nuint operator - ( nuint l , nuint r ) ;
public static nuint operator - ( uint l , nuint r ) ;
public static nuint operator ~ ( nuint value ) ;
public static nuint operator ++ ( nuint value ) ;
public static nuint operator -- ( nuint value ) ;
public static nuint operator * ( nuint l , nuint r ) ;
public static nuint operator * ( nuint l , uint r ) ;
public static nuint operator / ( nuint l , nuint r ) ;
public static nuint operator / ( nuint l , uint r ) ;
public static nuint operator % ( nuint l , nuint r ) ;
public static nuint operator % ( nuint l , uint r ) ;
public static nuint operator & ( nuint l , uint r ) ;
public static nuint operator & ( nuint l , nuint r ) ;
public static nuint operator | ( nuint l , uint r ) ;
public static nuint operator | ( nuint l , nuint r ) ;
public static nuint operator ^ ( nuint l , uint r ) ;
public static nuint operator ^ ( nuint l , nuint r ) ;
public static nuint operator << ( nuint l , nuint r ) ;
public static nuint operator << ( nuint l , uint r ) ;
public static nuint operator >> ( nuint l , nuint r ) ;
public static nuint operator >> ( nuint l , uint r ) ;
public static bool operator == ( nuint l , UIntPtr r ) ;
public static bool operator == ( UIntPtr l , nuint r ) ;
public static bool operator == ( nuint l , ulong r ) ;
public static bool operator == ( ulong l , nuint r ) ;
public static bool operator == ( nuint l , uint r ) ;
public static bool operator == ( nuint l , nuint r ) ;
public static bool operator == ( uint l , nuint r ) ;
public static bool operator != ( UIntPtr l , nuint r ) ;
public static bool operator != ( ulong l , nuint r ) ;
public static bool operator != ( nuint l , UIntPtr r ) ;
public static bool operator != ( nuint l , ulong r ) ;
public static bool operator != ( uint l , nuint r ) ;
public static bool operator != ( nuint l , uint r ) ;
public static bool operator != ( nuint l , nuint r ) ;
public static bool operator < ( UIntPtr l , nuint r ) ;
public static bool operator < ( nuint l , UIntPtr r ) ;
public static bool operator < ( ulong l , nuint r ) ;
public static bool operator < ( nuint l , ulong r ) ;
public static bool operator < ( uint l , nuint r ) ;
public static bool operator < ( nuint l , nuint r ) ;
public static bool operator < ( nuint l , uint r ) ;
public static bool operator > ( UIntPtr l , nuint r ) ;
public static bool operator > ( ulong l , nuint r ) ;
public static bool operator > ( nuint l , ulong r ) ;
public static bool operator > ( uint l , nuint r ) ;
public static bool operator > ( nuint l , uint r ) ;
public static bool operator > ( nuint l , nuint r ) ;
public static bool operator > ( nuint l , UIntPtr r ) ;
public static bool operator <= ( nuint l , uint r ) ;
public static bool operator <= ( UIntPtr l , nuint r ) ;
public static bool operator <= ( nuint l , UIntPtr r ) ;
public static bool operator <= ( ulong l , nuint r ) ;
public static bool operator <= ( nuint l , ulong r ) ;
public static bool operator <= ( uint l , nuint r ) ;
public static bool operator <= ( nuint l , nuint r ) ;
public static bool operator >= ( nuint l , nuint r ) ;
public static bool operator >= ( nuint l , UIntPtr r ) ;
public static bool operator >= ( ulong l , nuint r ) ;
public static bool operator >= ( nuint l , ulong r ) ;
public static bool operator >= ( uint l , nuint r ) ;
public static bool operator >= ( nuint l , uint r ) ;
public static bool operator >= ( UIntPtr l , nuint r ) ;
public static implicit operator ulong ( nuint value ) ;
public static implicit operator UIntPtr ( nuint value ) ;
public static implicit operator nuint ( uint value ) ;
public static implicit operator nuint ( UIntPtr value ) ;
public static explicit operator nuint ( ulong value ) ;
public static explicit operator uint ( nuint value ) ;
}
IntPtr
/ UIntPtr
vs nint
/ nuint
Baseado em:
Além de suportar conversão implícita para:
IntPtr
de/para nint
UIntPtr
de/para nuint
As conversões a seguir são suportadas em comparação com as conversões disponíveis para IntPtr
/ UIntPtr
.
Nome | De | IntPtr # | UIntPtr C# | nint C# | nuint C# |
---|---|---|---|---|---|
op_Implícito | int | N / D | N / D | Sim | N / D |
op_Implícito | uint | N / D | N / D | N / D | Sim |
op_Explícito | long | N / D | N / D | Sim | N / D |
op_Explícito | ulong | N / D | N / D | N / D | Sim |
Nome | Para | IntPtr # | UIntPtr C# | nint C# | nuint C# |
---|---|---|---|---|---|
op_Implícito | long | N / D | N / D | Sim | N / D |
op_Implícito | ulong | N / D | N / D | N / D | Sim |
op_Explícito | int | N / D | N / D | Sim | N / D |
op_Explícito | uint | N / D | N / D | N / D | Sim |
Nome | Símbolo do operador C++ | IntPtr # | UIntPtr C# | nint C# | nuint C# |
---|---|---|---|---|---|
op_Decremento | - 1 | N / D | N / D | Sim | Sim |
op_Incremento | ++ 1 | N / D | N / D | Sim | Sim |
op_UnaryNegation | - (unário) | N / D | N / D | Sim | Sim |
op_UnaryPlus | + (unário) | N / D | N / D | Sim | Sim |
op_LogicalNot | ! | N / D | N / D | N / D | N / D |
op_True | DE 2 | N / D | N / D | N / D | N / D |
op_Falso | DE 2 | N / D | N / D | N / D | N / D |
op_AddressOf | & (unário) | N / D | N / D | N / D | N / D |
op_OnesComplement | ~ | N / D | N / D | Sim | Sim |
op_PointerDereference | * (unário) | N / D | N / D | N / D | N / D |
1 Do ponto de vista do C++ puro, a maneira como essas funções devem ser escritas para a CLI difere em um aspecto muito importante. Em C++, esses métodos devem incrementar ou decrementar seu operando diretamente, enquanto, em CLI, não devem; em vez disso, eles simplesmente retornam o valor de seu operando +/- 1, conforme apropriado, sem modificar seu operando. O operando deve ser incrementado ou decrementado pelo compilador que gera o código do operador ++/--, separado da chamada a estes métodos.
2 Os operadores op_True e op_False não existem em C++. Eles são fornecidos para suportar tipos booleanos de três estados, como aqueles usados em linguagens de banco de dados.
A tabela abaixo mostra os tipos que podem ser usados junto com o tipo no cabeçalho para um determinado operador binário. Por padrão, isso é para o tipo no lado esquerdo ou direito da expressão (operador binário). No entanto, em alguns casos, um operador pode estar disponível apenas para um tipo em um lado específico da expressão, com (R)
significando "somente para a direita".
Nome | Símbolo do operador C++ | IntPtr # | UIntPtr C# | nint C# | nuint C# |
---|---|---|---|---|---|
op_Adição | + | int (R) | int (R) | nint , IntPtr , int | nuint , UIntPtr , uint |
op_Subtração | - | int (R) | int (R) | nint , IntPtr , int | nuint , UIntPtr , uint |
op_Multiplicar | * | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_Divisão | / | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_Módulo | % | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_ExclusivoOr | ^ | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_BitwiseAnd | & | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_BitwiseOr | | | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_LogicalAnd | && | N / D | N / D | N / D | N / D |
op_LogicalOr | || | N / D | N / D | N / D | N / D |
op_Assign | N/D (= não é o mesmo) | N / D | N / D | N / D | N / D |
op_LeftShift | << | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_RightShift | >> | N / D | N / D | nint , IntPtr , int (R) | nuint , UIntPtr , uint (R) |
op_SignedRightShift | N/D | N / D | N / D | N / D | N / D |
op_UnsignedRightShift | N/D | N / D | N / D | N / D | N / D |
op_Equalidade | == | IntPtr | UIntPtr | nint , IntPtr , int , long | nuint , UIntPtr , uint , ulong |
op_GreaterThan | > | IntPtr | UIntPtr | nint , IntPtr , int , long | nuint , UIntPtr , uint , ulong |
op_LessThan | < | IntPtr | UIntPtr | nint , IntPtr , int , long | nuint , UIntPtr , uint , ulong |
op_Desigualdade | != | IntPtr | UIntPtr | nint , IntPtr , int , long | nuint , UIntPtr , uint , ulong |
op_GreaterThanOrEqual | >= | IntPtr | UIntPtr | nint , IntPtr , int , long | nuint , UIntPtr , uint , ulong |
op_LessThanOrEqual | <= | IntPtr | UIntPtr | nint , IntPtr , int , long | nuint , UIntPtr , uint , ulong |
op_UnsignedRightShiftAssignment | Não definido | N / D | N / D | N / D | N / D |
op_MemberSelection | -> | N/A (N/OC#) | N/A (N/OC#) | N/A (N/OC#) | N/A (N/OC#) |
op_RightShiftAssignment | >>= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_MultiplicationAssignment | *= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_PointerToMemberSelection | ->* | N/A (N/OC#) | N/A (N/OC#) | N/A (N/OC#) | N/A (N/OC#) |
op_SubtractionAssignment | -= | int | int | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_ExclusiveOrAssignment | ^= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_LeftShiftAssignment | <<= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_ModulusAssignment | %= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_AdditionAssignment | += | int | int | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_BitwiseAndAssignment | &= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_BitwiseOrAssignment | |= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
op_Vírgula | , | N/A (N/OC#) | N/A (N/OC#) | N/A (N/OC#) | N/A (N/OC#) |
op_DivisionAssignment | /= | N/A (N/OC#) | N/A (N/OC#) | nint , IntPtr , int | nuint , UIntPtr , uint TODO |
N/A
= Não disponívelN/O
= Não sobrecarregável, mas para operadores de atribuição disponíveis via operador binário estático, por exemplo +=
está disponível via +
. Os métodos a seguir têm o mesmo ou encaminham para a implementação IntPtr
/ UIntPtr
equivalente.
public override bool Equals ( object obj )
public override int GetHashCode ( )
public override string ToString ( )
As seguintes interfaces foram implementadas para o tipo nint
ou nuint
fornecido:
IEquatable < T >
IComparable < T >