Creating extension methods for IntPtr?

I am trying to port third party code and there are references to IntPtr.Zero and IntPtr.Size which trigger an error No static method “Zero” on “IntPtr” or No static method “Size” on “IntPrt” respectively.

Looking online someone said that IntPtr.Zero was the equivalent of new IntPtr(0) but but this also gives me an error, which reads Type “NativeInt” has no accessible constructors.

My next thought was to try to create extension methods for IntPtr with these methods.

    public static class IntPtrExtensions
    {
        public static IntPtr Zero(this IntPtr Ptr)
        {
            return 0; //new IntPtr(0);
        }

        public static int Size(this IntPtr Ptr)
        {
            return sizeof(int);
        }
    }

Even with these I still get the error stating that there is no static method of that name.

How do I create extension methods for IntPtr?
And what are the RO C# equivalents of IntPtr.Zero and IntPtr.Size?

IIRC, classic VC#-style extension methods require some attribute to make them as extension. that said, yo can just use Elements’ extension class syntax:

public __extension class IntPtrExtensions : IntPtr
    {
        public static IntPtr Zero()
        {
            return 0; //new IntPtr(0);
        }

        public static int Size
        {
            return sizeof(IntPtr);
        }
    }
1 Like