Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the size of a class in C#

Tags:

public class A {   int x;   float y; } 

How to find the size of the class in C#. Is there any operator like Sizeof(), which used to be in C++

like image 236
Sunil Avatar asked Feb 25 '10 05:02

Sunil


People also ask

What is the size of a class in programming?

The size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses.

What is the size of an object of a class?

The size of object of a class depends on the no. of bytes occupied by the data members of the class. }; The object of class student will occupy space of 8 bytes.


1 Answers

The following would give you the size of the C# class:

Marshal.SizeOf(typeof(Myclass));  using System.Runtime.InteropServices;  [StructLayout(LayoutKind.Sequential)] class Myclass {  } 

Remember size of a class depends on padding.

like image 147
Ritesh Avatar answered Oct 22 '22 18:10

Ritesh