Size of a Class
-
Hi all, I have a class public class Abc { private UInt32 x; private UInt32 y; private Int32 z; }; I need to get the size of the class. Please help! Thanks in Advance. San
Why, what possible relevance is the size of the file. Once it is compiled it is part of an assembly and does not have a "size". If you want the number of characters in the file open it in word or open it using a streamreader and read in tha lines,counting the characters, file size use io.fileinfo
Never underestimate the power of human stupidity RAH
-
Hi all, I have a class public class Abc { private UInt32 x; private UInt32 y; private Int32 z; }; I need to get the size of the class. Please help! Thanks in Advance. San
You cannot get unmanaged size of a class. See this example : Abc abc = new Abc(); int size = System.Runtime.InteropServices.Marshal.SizeOf(abc); Console.WriteLine(size); If Abc is a class, then you get an exception. If you change Abc to a struct, then you get a size (12).
-
Hi all, I have a class public class Abc { private UInt32 x; private UInt32 y; private Int32 z; }; I need to get the size of the class. Please help! Thanks in Advance. San
-
hi you can use the Marshal.SizeOf(typeof(MyClass)) method in the System.Runtime.InteropServices namespace. regards
When I given as per your suggestion I got an exception as follows. An unhandled exception of type 'System.ArgumentException' occurred in exe Additional information: Type 'class' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
-
You cannot get unmanaged size of a class. See this example : Abc abc = new Abc(); int size = System.Runtime.InteropServices.Marshal.SizeOf(abc); Console.WriteLine(size); If Abc is a class, then you get an exception. If you change Abc to a struct, then you get a size (12).
-
Using some memory profiling tools. BTW, could you tell why you are worried with the size?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Hi all, I have a class public class Abc { private UInt32 x; private UInt32 y; private Int32 z; }; I need to get the size of the class. Please help! Thanks in Advance. San
San wrote:
I need to get the size of the class.
Why? IIRC, a class like that would use 20 bytes on a 32 bit system and 28 bytes on a 64 bit system. Each object consists of two pointers plus the instance data. As you see, the size differs depending on the version of the CLR (which determines the memory layout). Also, you have a pretty simple example. If you have members of different data types, they may be aligned on an even word boundary which may add to the size, and they might get rearranged to make best use of memory when aligned. So, it might not be possible to predict exactly how large the object can be.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Monday, September 8, 2008 9:31 AM
-
When I given as per your suggestion I got an exception as follows. An unhandled exception of type 'System.ArgumentException' occurred in exe Additional information: Type 'class' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
San wrote:
no meaningful size or offset can be computed.
I think you now have the answer to your question.
Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog
-
Hi all, I have a class public class Abc { private UInt32 x; private UInt32 y; private Int32 z; }; I need to get the size of the class. Please help! Thanks in Advance. San
[StructLayout(LayoutKind.Sequential)]
public class Abc
{
private UInt32 x;
private UInt32 y;
private Int32 z;
};int size = Marshal.SizeOf(typeof(Abc));
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You cannot get unmanaged size of a class. See this example : Abc abc = new Abc(); int size = System.Runtime.InteropServices.Marshal.SizeOf(abc); Console.WriteLine(size); If Abc is a class, then you get an exception. If you change Abc to a struct, then you get a size (12).
stancrm wrote:
You cannot get unmanaged size of a class.
You can if you specify the class' layout (explicit or sequential). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: