16

When I wrote the following snippet for experimenting purposes, it raised the hover-error (see screenshot):

Cannot declare pointer to non-unmanaged type 'dynamic'

The snippet:

dynamic* pointerToDynamic = &fields;

While the code is clearly not allowed (you cannot take the address of a managed type), it raised with me the question: what is a non-unmanaged type and how is it different to a managed type? Or is it just Visual Studio trying to be funny?

enter image description here

Abel
  • 56,041
  • 24
  • 146
  • 247
  • 7
    Clearly a non-unmanaged type is a non-non-non-unmanaged type. Mmmm. – Kent Boogaart Mar 30 '12 at 10:37
  • 3
    Don't read too much into a clumsy error message. The C# binder simply doesn't support pointers. – Hans Passant Mar 30 '12 at 13:10
  • 3
    There is no difference. The wording is simply due to the expected behavior. "Pointers are for unmanaged type. This is not one of those. It is a non-unmanaged type." The error is not that fields is managed, it's that fields is *not unmanaged*, even though those two phrases mean the same thing. – Michael Edenfield Mar 30 '12 at 13:54
  • Just remember: If a program wants to be told to go left, and you tell it to go right, it can give you a non-left error. – Justin Time - Reinstate Monica Jun 30 '16 at 21:22
  • 1
    @JustinTime, funny point, but the analogy here would be, "if the program tells you _not_ to go left, and you go left, it gives you an un-not-left error" ;)))) – Abel Jul 01 '16 at 12:36

2 Answers2

8

There is a difference between unmanaged and non-managed pointers.

A managed pointer is a handle to an object on the managed heap, and AFAIK is available in managed C++ only. It is equivalent of C# reference to an object. Unmanaged pointer, on the other hand, is equivalent of a traditional C-style pointer, i.e. address of a memory location; C# provides unary & operator, fixed keyword and unsafe context for that.

You are trying to get a pointer to a managed field (dynamic is actually System.Object is disguise), while C# allows pointers to unmanaged objects only, hence the wording: your type is non-unmanaged.

A bit more on this here.

Update: to make it more clear, managed C++ supports classic C-style pointers and references. But to keep C++ terminology consistent, they are called unmanaged and managed pointers, correspondingly. C# also supports pointers (explicitly in unsafe context) and references (implicitly whenever objects of reference types are involved), but the latter is not called "managed pointers", they are just references.

To sum up: in C++ there are unmanaged and managed pointers, in C# - unmanaged pointers and references.

Hope it makes sense now.

Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
  • That makes more sense in light of the error. I never realized unmanaged and non-managed were different things (and it seems you only got to explain the difference between managed/unmanaged pointers, typo?). But considering your last paragraph, that seems a reasonable explanation. – Abel Mar 30 '12 at 12:47
  • @Abel: I updated my answer, sorry if it was not clear. Maybe it's a bit better now. – Igor Korkhov Mar 30 '12 at 13:17
  • I'm afraid you repeated what you already said ;). In your first line you refer to difference between **un** managed and **non-** managed pointers. The latter is then not mentioned in your text anymore. I'm aware of the difference between (un)managed pointers. – Abel Mar 30 '12 at 13:25
  • @Abel: Oh, now I understand, thanks. The phrase was indeed misleading. – Igor Korkhov Mar 30 '12 at 13:40
2

You cannot create a pointer to a managed type. While int, double, etc are managed, they have unmanaged counterparts.

So what non-unmanaged type really means is the managed type.

The problem here is that the managed type since is sitting on the heap, you cannot get a pointer to. You can get a pointer using fixed keyword but that is mainly for arrays.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Yes, I'm aware of that, thanks. What I was really curious about is the wording here. Why would IntelliSense call it a non-unmanaged type, when it means a managed type? In all other related errors, it is simply called a managed type. Is there really no difference? Semantically: Clean, Unclean and Non-unclean are all different. Non-unmanaged? – Abel Mar 30 '12 at 10:41