C # implements a system dependent integer

I need to define a system dependent integer type to be compatible with some low-level libraries. I installed x86 and x64 Project-Configuration and defined compilation conditional characters (IA32 and INTEL64) for them.

So, I would like to do the following:

#if IA32 typedef int SysInt; typedef uint SysUInt; #elif INTEL64 typedef long SysInt; typedef ulong SysUInt; #endif 

However, this does not work due to typedef, not available in C #. What is the best option for this?

Thanks in advance. Best wishes.

+4
source share
4 answers

You want IntPtr and UIntPtr , which are 32 bits in 32-bit processes and 64 bits in 64-bit processes. Since these types automatically accept the β€œbitness” process, there is no need for conditional compilation or two different project configurations to use them.

If you really want to use math for values, you must assign them to long or ulong to do the math, and then return to IntPtr or UIntPtr to pass them to external libraries.

+11
source

You can use aliases:

 #if IA32 using SysInt = System.Int32; #elif INTEL64 using SysInt = System.Int64; #endif 
+3
source

It seems you could do something like this, at least:

 #if IA32 using SysInt = System.Int32; using SysUInt = System.UInt32; #elif INTEL64 using SysInt = System.Int64; using SysUInt = System.UInt64; #endif 

It just pseudo-types for you. I do not know how useful this is for your purposes, but it is something.

+3
source
 #if IA32 public struct Sys { public int Int { get; set; } } #elif INTEL64 public struct Sys { public long Int { get; set; } } #endif 
0
source

Source: https://habr.com/ru/post/1343725/


All Articles