Jak sprawdzić rozmiar struktury w C#?

0

Dlaczego taki kod nie działa? o.O

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(sizeof(int));

            Console.WriteLine(sizeof(Point));

            Console.ReadKey();
        }
    }

    struct Point
    {
        public int X { get; set; }
        public int Y { get; set; }
    }

VS pokazuje, że 'Point' does not have a predefined size, therefore sizeof can only be used in an unsafe context. Czytałem Google'a, ale dalej nie rozumiem, szczególnie że sizeof(int) zwraca u mnie 4.

1
Point point = new Point();
System.Runtime.InteropServices.Marshal.SizeOf(point).ToString(); // 8
2

@nobody01: kod Ci nie działa, bo nie czytasz dokumentacji. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sizeof

Starting with version 2.0 of C#, applying sizeof to simple or enum types no longer requires that code be compiled in an unsafe context. (...) For all other types, including structs, the sizeof operator can be used only in unsafe code blocks.

1

Tak w skrócie . w przypadku Int32 rozmiar struktury znany jest w czasie kompilacji
a w przypadku mojej struktury dopiero po uruchomieniu programu

using System;


namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                int rozmiar = sizeof(A);
                Console.WriteLine(rozmiar);
            }      
        }
    }

    struct  A
    {
        int a;
        int b;
    }
}
using System;
namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rozmiar = System.Runtime.InteropServices.Marshal.SizeOf(typeof(A));
            Console.WriteLine(rozmiar);       
        }
    }


    struct  A
    {
        int a;
        int b;
        int c;
    }
}

.method assembly hidebysig static int32 SizeOfHelper(class System.Type t, bool throwIfNotMarshalable) cil managed internalcall
{
.custom instance void System.Security.SecuritySafeCriticalAttribute::.ctor() = ( 01 00 00 00 )
} // end of method Marshal::SizeOfHelper

internalcall specifies that the method body is provided by this CLI (and is typically used by low-level
methods in a system library). It shall not be applied to methods that are intended for use across implementations of the CLI.

**sizeof typeTok Push the size, in bytes, of a type as an unsigned int32
**
Returns the size, in bytes, of a type. typeTok can be a generic parameter, a reference type or a
value type.
For a reference type, the size returned is the size of a reference value of the corresponding type,
not the size of the data stored in objects referred to by a reference value.
[Rationale: The definition of a value type can change between the time the CIL is generated and
the time that it is loaded for execution. Thus, the size of the type is not always known when the
CIL is generated. The sizeof instruction allows CIL code to determine the size at runtime
without the need to call into the Framework class library. The computation can occur entirely at
runtime or at CIL-to-native-code compilation time. sizeof returns the total size that would be
occupied by each element in an array of this type – including any padding the implementation
chooses to add. Specifically, array elements lie sizeof

1 użytkowników online, w tym zalogowanych: 0, gości: 1