@kodwi
https://moikrug.ru/kodwi

Почему sizeof(System.Char) == 2, а Marshal.SizeOf(System.Char) == 1?

В чем заключается данная неоднозначность в определении размера типа Char в C#?
  • Вопрос задан
  • 2539 просмотров
Решения вопроса 1
NekitoSP
@NekitoSP
msdn.microsoft.com/en-us/library/eahchzkf.aspx
Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any padding.

и:
msdn.microsoft.com/en-us/library/vstudio/5s4920fa(...
The size of the specified type in unmanaged code.

т.е. sizeof - размер типов в CLR, а Marshal.SizeOf - размер типа в unmanaged коде.

И по первой ссылке есть пометка по этому поводу:
Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы