3 ms·
> You can't store a raw pointer You can if you mark the containing class/struct "unsafe", which allows both type safety and increased ease of use.
by MaulingMonkey 13y ago
> You can't store a raw pointer
You can if you mark the containing class/struct "unsafe", which allows both type safety and increased ease of use.
- kevingadd 13y agoHmmm, I've never seen this done. Thanks for the heads-up, I'll have to try it out. Are you sure you can do it with classes? I would not be shocked by being able to do it with structs.
- MaulingMonkey 13y agoSure am. using System; namespace Test2012 { unsafe class C { public byte* P; public override string ToString( ) { return new IntPtr(P).ToString( ); } } unsafe struct S { public byte* P; public override string ToString( ) { return new IntPtr(P).ToString( ); } } static class Program { static unsafe void Main( ) { fixed( byte* test = new byte[] { 1, 2, 3 } ) { var c = new C( ) { P = test }; var s = new S( ) { P = test }; Console.WriteLine( "Test: {0}", c ); Console.WriteLine( "Test: {0}", s ); } } } } Result: Test: 40510364 Test: 40510364 Press any key to continue . . .