locked
get array length like in C++ RRS feed

  • Question

  • Hi guys!

    I want to get the size of my array like I do it in C++, but in C# - I Can't do the same.

    Look at the code:

    byte[] data = cry.ComputeHash(Encoding.UTF8.GetBytes("123456"));
          
     for (int i = 0; i < sizeof(data)/sizeof(byte); i++)
    {
            
    };
    


    Why the data in the sizeof() operator isn't available in for statement?

     

    Best Regards,

    Oleg

    Friday, August 12, 2011 9:33 AM

Answers

  • "Why I can't use the "sizeof" operator like this way and this regard to thу pointers."

    Because it would be the wrong thing to do. The language/framework provides a well known way to get the number of elements of an array, use that, not C++ way. This is C# after all.

    If sizeof would be allowed in this case then there would be ambiguity about what size it returns:

    • the size of all array elements
    • the size of all array elements + size of array length field
    • the size of all array elements + size of array length field + size of method table pointer
    • the size of all array elements + size of array length field + size of method table pointer + size of object header

    That's why the C# spec allows sizeof to be used only on "unmanaged types" - a byte[] is not an unmanaged type.

    In addition, the size of managed types is not defined at compile time, it's defined only at runtime. And even at runtime you're not supposed to rely on it except in interop scenarios. 

    • Proposed as answer by Carl Daniel Sunday, August 14, 2011 2:53 AM
    • Marked as answer by Paul Zhou Wednesday, August 24, 2011 6:07 AM
    Friday, August 12, 2011 1:06 PM

All replies

  • Because there's no need to do that. Use the Length property of the array:

    for (int i = 0; i < data.Length; i++)

    • Proposed as answer by DiegoCattaruzza Friday, August 12, 2011 10:37 AM
    • Unproposed as answer by magesi Friday, August 12, 2011 12:33 PM
    • Proposed as answer by Michael Stacie Friday, August 12, 2011 10:26 PM
    • Unproposed as answer by magesi Thursday, August 18, 2011 8:44 AM
    Friday, August 12, 2011 10:13 AM
  • Ability is not equaled to necessity!

    I was asking ony about ability.

    Why I can't use the "sizeof" operator like this way and this regard to thу pointers.

    You can use pointer to go throught array values like *p + i , where i - is incrementing variable.

    I care only about ability.

    Friday, August 12, 2011 12:39 PM
  • "Why I can't use the "sizeof" operator like this way and this regard to thу pointers."

    Because it would be the wrong thing to do. The language/framework provides a well known way to get the number of elements of an array, use that, not C++ way. This is C# after all.

    If sizeof would be allowed in this case then there would be ambiguity about what size it returns:

    • the size of all array elements
    • the size of all array elements + size of array length field
    • the size of all array elements + size of array length field + size of method table pointer
    • the size of all array elements + size of array length field + size of method table pointer + size of object header

    That's why the C# spec allows sizeof to be used only on "unmanaged types" - a byte[] is not an unmanaged type.

    In addition, the size of managed types is not defined at compile time, it's defined only at runtime. And even at runtime you're not supposed to rely on it except in interop scenarios. 

    • Proposed as answer by Carl Daniel Sunday, August 14, 2011 2:53 AM
    • Marked as answer by Paul Zhou Wednesday, August 24, 2011 6:07 AM
    Friday, August 12, 2011 1:06 PM
  • http://stackoverflow.com/questions/605621/how-to-get-object-size-in-memory
    Please mark this post as answer if it solved your problem. Happy Programming!
    Saturday, August 13, 2011 5:46 AM
  • Check for Marshal object
    Mark Answered, if it solves your question
    Rohit Arora
    Wednesday, August 17, 2011 11:20 AM