Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,683 questions

55,435 answers

573 users

How to get the number of bytes of a variable in C#

1 Answer

0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        static void DisplaySizeOf<T>(int n) =>
                    Console.WriteLine($"{typeof(T)} is {n} byte(s)");

        Console.WriteLine("C# types:");
        DisplaySizeOf<byte>(sizeof(byte));
        DisplaySizeOf<char>(sizeof(char));
        DisplaySizeOf<bool>(sizeof(bool));
        DisplaySizeOf<short>(sizeof(short)); // Int16
        DisplaySizeOf<ushort>(sizeof(ushort)); // UInt16
        DisplaySizeOf<int>(sizeof(int)); // Int32
        DisplaySizeOf<uint>(sizeof(uint)); // UInt32
        DisplaySizeOf<long>(sizeof(long)); // Int64
        DisplaySizeOf<ulong>(sizeof(ulong)); // UInt64
        DisplaySizeOf<float>(sizeof(float)); // Single
        DisplaySizeOf<double>(sizeof(double));
        DisplaySizeOf<decimal>(sizeof(decimal));
    }
}


/*
run:

C# types:
System.Byte is 1 byte(s)
System.Char is 2 byte(s)
System.Boolean is 1 byte(s)
System.Int16 is 2 byte(s)
System.UInt16 is 2 byte(s)
System.Int32 is 4 byte(s)
System.UInt32 is 4 byte(s)
System.Int64 is 8 byte(s)
System.UInt64 is 8 byte(s)
System.Single is 4 byte(s)
System.Double is 8 byte(s)
System.Decimal is 16 byte(s)

*/

 



answered Jun 4, 2025 by avibootz

Related questions

1 answer 109 views
1 answer 115 views
1 answer 216 views
1 answer 143 views
1 answer 176 views
...