Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Size of a Class

Size of a Class

Scheduled Pinned Locked Moved C#
help
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S San 0

    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

    M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #2

    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

    1 Reply Last reply
    0
    • S San 0

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      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).

      S M 2 Replies Last reply
      0
      • S San 0

        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

        J Offline
        J Offline
        JoeSharp
        wrote on last edited by
        #4

        hi you can use the Marshal.SizeOf(typeof(MyClass)) method in the System.Runtime.InteropServices namespace. regards

        S 1 Reply Last reply
        0
        • J JoeSharp

          hi you can use the Marshal.SizeOf(typeof(MyClass)) method in the System.Runtime.InteropServices namespace. regards

          S Offline
          S Offline
          San 0
          wrote on last edited by
          #5

          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.

          C 1 Reply Last reply
          0
          • L Lost User

            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).

            S Offline
            S Offline
            San 0
            wrote on last edited by
            #6

            Yes I got an exception since Abc is a class. So how is to calculate the size?

            N 1 Reply Last reply
            0
            • S San 0

              Yes I got an exception since Abc is a class. So how is to calculate the size?

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #7

              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

              1 Reply Last reply
              0
              • S San 0

                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

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #8

                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

                1 Reply Last reply
                0
                • S San 0

                  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.

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #9

                  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

                  1 Reply Last reply
                  0
                  • S San 0

                    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

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #10

                    [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:

                    1 Reply Last reply
                    0
                    • L Lost User

                      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).

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #11

                      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:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups