Wrox Programmer Forums
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 1st, 2005, 01:59 PM
Registered User
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default sizeof

I have a class in which I have, lets say 8 int members;
I need to know the size of that class ( which is now 8 * 4 bytes ).
In C, we used sizeof( myClass ), but I can't find how to do it in C#.
Any idea how to do it the .Net way ? ( besides using unsafe and sizeof )

Old April 2nd, 2005, 08:01 AM
Friend of Wrox
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post

sizeof in C# just used for valuetypes in unsafe mode(in version 2 no neeed for unsafe mode)

I don't know what could be good for this?

you can try GC.GetTotalMemory Method after and befor creating your object,(but you should know other threads may run concurrently and that method doesn't give you a proper result)

I myself tried something now
class simple
    int a=0;
    int c=0;
private double size()
    double c=GC.GetTotalMemory(true);
    simple s=new simple();
    return GC.GetTotalMemory(true)-c;
it gives me 16 bytes if I consider just one integer field it gives me 12 bytes that 4 bytes was for an integer field(the sizeof(int) is 4 bytes)

software student.

Similar Threads
Thread Thread Starter Forum Replies Last Post
SizeOf Structs CDMBR C# 1 February 21st, 2007 06:24 PM
sizeof Integer in infopath vikram_vikram Infopath 0 November 30th, 2005 08:42 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.