Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 270521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:00:49+00:00 2026-05-12T00:00:49+00:00

Goal: to programmatically determine the sizes (in bytes) of the fields of a class.

  • 0

Goal: to programmatically determine the sizes (in bytes) of the fields of a class.
For example, see the comments below …

class MyClass
    {
    public  byte    b ;
    public  short   s ;
    public  int i ;
    }

class MainClass
    {
    public static void Main()
        {
        foreach ( FieldInfo fieldInfo
            in typeof(MyClass).GetFields(BindingFlags.Instance
             | BindingFlags.Public | BindingFlags.NonPublic) )
            Console.WriteLine ( fieldInfo.FieldType ) ;

        // output is:
        //    System.Byte
        //    System.Int16
        //    System.Int32

        // desired: to include "sizeof" each type (in bytes) ...
        //    System.Byte     1
        //    System.Int16    2
        //    System.Int32    4
        }
    }
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:00:50+00:00Added an answer on May 12, 2026 at 12:00 am

    You simply want to use the Marshal.SizeOf method in the System.Runtime.InteropServices namespace.

    foreach (var fieldInfo in typeof(MyClass).GetFields(BindingFlags.Instance |
        BindingFlags.Public | BindingFlags.NonPublic))
    {
        Console.WriteLine(Marshal.SizeOf(fieldInfo.FieldType));
    }
    

    Do however take note of the following paragraph in the Remarks section:

    The size returned is the actually the size of the unmanaged type. The unmanaged and managed sizes of an object can differ. For character types, the size is affected by the CharSet value applied to that class.

    These differences are probably inconsequential though, depending on your purpose… I’m not even sure it’s possibly to get the exact size in managed memory (or at least not without great difficulty).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
My ultimate goal is to do this programmatically, but as a sanity check I'm
I'm programmatically changing the margin of a layout inside a framelayout. My goal is
My goal is to find out how to programmatically adjust permissions on files &
My goal is to get a list of eBay categories programmatically. It appears that
Goal Take a class named Item and output its serialized XML as: <Template><!--some properties
GOAL: I'm making a UDP socket to send and receive data. I am testing
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
goal: I have the string 1234432144 I want to only replace the first 2
Goal Pass images generated by one process efficiently and at very high speed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.