কম্পিউটার টিউটোরিয়াল

C#-এ sizeof ব্যবহার না করে ভেরিয়েবল সাইজ বের করা

C#-এ sizeof অপারেটর শুধু আনম্যানেজড টাইপের (primitive types, structs) জন্য কাজ করে এবং `unsafe` কনটেক্সট দরকার হয় না (কিছু কেস বাদে)। কিন্তু `sizeof` ছাড়া সাইজ বের করার জন্য BitConverter.GetBytes বা Marshal.SizeOf ব্যবহার করা যায়।

পদ্ধতি ১: BitConverter.GetBytes (সিম্পল টাইপের জন্য)


using System;

public class Demo
{
    public static void Main()
    {
        int x = 42;
        
        // sizeof ব্যবহার করে
        int sizeWithSizeof = sizeof(int);
        Console.WriteLine($"sizeof(int) = {sizeWithSizeof}"); // 4
        
        // sizeof ছাড়া: BitConverter.GetBytes
        byte[] bytes = BitConverter.GetBytes(x);
        int sizeWithoutSizeof = bytes.Length;
        Console.WriteLine($"BitConverter.GetBytes length = {sizeWithoutSizeof}"); // 4
        
        // অন্যান্য টাইপের জন্য
        Console.WriteLine($"double: {BitConverter.GetBytes(1.0).Length}");      // 8
        Console.WriteLine($"bool: {BitConverter.GetBytes(true).Length}");        // 1
        Console.WriteLine($"long: {BitConverter.GetBytes(1L).Length}");          // 8
        Console.WriteLine($"float: {BitConverter.GetBytes(1.0f).Length}");       // 4
    }
}

আউটপুট


sizeof(int) = 4
BitConverter.GetBytes length = 4
double: 8
bool: 1
long: 8
float: 4

পদ্ধতি ২: Marshal.SizeOf (Struct এবং ক্লাসের জন্য)


using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
    public int Id;
    public double Value;
    public bool Flag;
}

public class MarshalSizeDemo
{
    public static void Main()
    {
        MyStruct s = new MyStruct { Id = 1, Value = 3.14, Flag = true };
        
        // Marshal.SizeOf - আনম্যানেজড মেমরি লেআউট অনুযায়ী সাইজ
        int size = Marshal.SizeOf(s);
        Console.WriteLine($"Struct সাইজ (Marshal): {size} বাইট");
        
        // sizeof স্ট্রাক্টের জন্য (unsafe দরকার)
        // unsafe { int size2 = sizeof(MyStruct); }
        
        // অ্যারে সাইজ
        int[] arr = new int[10];
        Console.WriteLine($"অ্যারে লেন্থ: {arr.Length}, টোটাল বাইট: {arr.Length * sizeof(int)}");
    }
}

পদ্ধতি ৩: ব্লিটেবল (Blittable) টাইপ চেক


using System;
using System.Runtime.InteropServices;

public static class SizeHelper
{
    public static int GetSize<T>(T value) where T : struct
    {
        if (IsBlittable(typeof(T)))
        {
            return Marshal.SizeOf(value);
        }
        else
        {
            // নন-ব্লিটেবল টাইপ (string, object রেফারেন্স ইত্যাদি) এর সাইজ অনির্ধারিত
            throw new ArgumentException($"{typeof(T)} blittable নয়");
        }
    }
    
    static bool IsBlittable(Type type)
    {
        // সিম্পল চেক: primitive, enum, struct with only blittable fields
        if (type.IsPrimitive || type.IsEnum) return true;
        if (!type.IsValueType) return false;
        
        foreach (var field in type.GetFields(
            System.Reflection.BindingFlags.Instance | 
            System.Reflection.BindingFlags.Public | 
            System.Reflection.BindingFlags.NonPublic))
        {
            if (!IsBlittable(field.FieldType)) return false;
        }
        return true;
    }
}

পদ্ধতি ৪: আবজেক্ট হেডার সহ ম্যানেজড সাইজ (প্রোফাইলিং용)


// নোট: এটি ম্যানেজড হিপে আকার, marais.SizeOf নয়
// সঠিক ম্যানেজড সাইজ পাওয়া কঠিন (GC, অ্যালাইনমেন্ট, অবজেক্ট হেডার)
public static long GetManagedSize(object obj)
{
    if (obj == null) return 0;
    
    // অনুমানমূলক: অবজেক্ট হেডার (৮/১৬ বাইট) + ফিল্ড সাইজ + প্যাডিং
    // সঠিক মাপের জন্য প্রোফাইলার (dotMemory, PerfView) ব্যবহার করুন
    return System.Runtime.InteropServices.Marshal.SizeOf(obj);
}

টাইপ অনুযায়ী সাইজ রেফারেন্স (৬৪-বিট প্রসেসে)

টাইপsizeof / BitConverterম্যানেজড হিপে (অনুমান)
bool11 (+প্যাডিং)
byte/sbyte11
short/ushort22
int/uint44
long/ulong88
float44
double88
char2 (UTF-16)2
IntPtr/UIntPtr8 (৬৪-বিট), 4 (৩২-বিট)8/4
object referenceN/A8 (৬৪-বিট), 4 (৩২-বিট)
object headerN/A8 (৬৪-বিট), 4 (৩২-বিট)

সারসংক্ষেপ

  • Primitive types: BitConverter.GetBytes(value).Length বা sizeof
  • Structs (Blittable): Marshal.SizeOf<T>()
  • Managed objects: সঠিক সাইজ পাওয়া কঠিন, প্রোফাইলার ব্যবহার করুন
  • Runtime-এ সাইজ: `Marshal.SizeOf` আনম্যানেজড রিপ্রেজেন্টেশনের সাইজ দেয়, ম্যানেজড হিপ সাইজ নয়
  1. C#-এ জ্যাগড অ্যারে (Jagged Array) এর দৈর্ঘ্য বের করা

  2. C#-এ ফোল্ডার সাইজ কীভাবে ক্যালকুলেট করবেন?

  3. C#-এ sizeof() অপারেটর দিয়ে ডেটা টাইপ বা ভেরিয়েবলের আকার জানা

  4. Python-এ একটি সংখ্যার ফ্যাক্টোরিয়াল (Factorial) বের করা