Udostępnij za pośrednictwem


Compiler Error CS0208

Cannot take the address of, get the size of, or declare a pointer to a managed type ('type')

Even when used with the unsafe keyword, taking the address of a managed object, getting the size of a managed object, or declaring a pointer to a managed type is not allowed. A managed type is:

  • any reference type

  • any struct that contains a reference type as a field or property

For more information, see Unsafe Code and Pointers (C# Programming Guide) and sizeof.

Example

The following sample generates CS0208:

// CS0208.cs
// compile with: /unsafe

class myClass
{
    public int a = 98;
}

struct myProblemStruct
{
    string s;
    float f;
}

struct myGoodStruct
{
    int i;
    float f;
}

public class MyClass
{
    unsafe public static void Main()
    {
        // myClass is a class, a managed type.
        myClass s = new myClass();  
        myClass* s2 = &s;    // CS0208

        // The struct contains a string, a managed type.
        int i = sizeof(myProblemStruct); //CS0208
        
        // The struct contains only value types.
        i = sizeof(myGoodStruct); //OK
        
    }
}

See Also

Reference

sizeof (C# Reference)

Change History

Date

History

Reason

October 2008

Added text and new code example.

Customer feedback.