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

5
  • 7
    Clearly a non-unmanaged type is a non-non-non-unmanaged type. Mmmm. Commented Mar 30, 2012 at 10:37
  • 3
    Don't read too much into a clumsy error message. The C# binder simply doesn't support pointers. Commented Mar 30, 2012 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. Commented Mar 30, 2012 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. Commented Jun 30, 2016 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
    Commented Jul 1, 2016 at 12:36

2 Answers 2

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.

4
  • 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
    Commented Mar 30, 2012 at 12:47
  • @Abel: I updated my answer, sorry if it was not clear. Maybe it's a bit better now. Commented Mar 30, 2012 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
    Commented Mar 30, 2012 at 13:25
  • @Abel: Oh, now I understand, thanks. The phrase was indeed misleading. Commented Mar 30, 2012 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.

1
  • 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
    Commented Mar 30, 2012 at 10:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.