开发者

How to declare a IntPtr?

开发者 https://www.devze.com 2022-12-23 02:44 出处:网络
I have a Window handle Picker and it says my handle is 0094167C. When I declare the variable in c# the letter in th开发者_JAVA技巧is code gives an error.

I have a Window handle Picker and it says my handle is 0094167C. When I declare the variable in c# the letter in th开发者_JAVA技巧is code gives an error. How to declare?

public const IntPtr WinHandle = 0094167C;


You know that the handle will typically change with each application and/or system start? This means your constant is subject to failure anyway.

If, however, you really want to assign a constant other than zero to an IntPtr (which would be IntPtr.Zero), the documentation states that there are constructors that take Int32, Int64 or Void* as parameter.


As OregonGhost points out you probably don't want to do that for a windows handle. However, for a IntPtr in general, what you can do is this static readonly fields:

static readonly IntPtr TenK = new IntPtr(1024 * 10000);


I'd like to add to AgnosticOracle's answer.

In addition to constant-like static-readonly IntPtr/UIntPtr variables, you can use nint and nuint which are backed by IntPtr and UIntPtr, respectively.

These types can have integer and unsigned integer values assigned to them and can also be defined as constants.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#native-sized-integers

0

精彩评论

暂无评论...
验证码 换一张
取 消