I was reading MSDN docs about what's new in C# 7.x to 8.0 and found these articles (doc1, doc2)
so I decided to create a little test.
Pointer.cs
internal readonly unsafe struct Pointer<T>
where T : unmanaged
{
private readonly void* mValue;
internal Pointer(T value)
{
//fixed (T* val = &value) <--- Error: You cannot use the fixed statement to take the adress of an already fixed expression.
// mValue = val;
mValue = &value;
}
public static implicit operator Pointer<T>(T value)
{
return new Pointer<T>(value);
}
public static implicit operator string(Pointer<T> value)
{
var ptr = (T*)value.mValue;
return ptr->ToString(); // returns random values (maybe adresses).
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
Pointer<int> ptr = 2;
Console.WriteLine(ptr); // prints random values (maybe adresses).
}
}
why am I not allowed to use fixed
statement in my constructor. I mean it makes sense, that it throws an error. I have said that T
should be unmanaged
, so I think it automatically uses fixed
keyword internally. BUT if it does so then why am I getting random values.
According to the documentation for unmanaged types T will be some kind of value type that cannot contain references to managed types. As such it will be allocated on the stack. When the constructor returns the stack will be popped and the pointer will be pointing to some undefined value. You cannot use fixed
for something allocated on the stack since cannot be moved by the garbage collector anyway, and is therefore considered 'fixed'.