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 | ম্যানেজড হিপে (অনুমান) |
|---|---|---|
| bool | 1 | 1 (+প্যাডিং) |
| byte/sbyte | 1 | 1 |
| short/ushort | 2 | 2 |
| int/uint | 4 | 4 |
| long/ulong | 8 | 8 |
| float | 4 | 4 |
| double | 8 | 8 |
| char | 2 (UTF-16) | 2 |
| IntPtr/UIntPtr | 8 (৬৪-বিট), 4 (৩২-বিট) | 8/4 |
| object reference | N/A | 8 (৬৪-বিট), 4 (৩২-বিট) |
| object header | N/A | 8 (৬৪-বিট), 4 (৩২-বিট) |
সারসংক্ষেপ
- Primitive types:
BitConverter.GetBytes(value).Lengthবাsizeof - Structs (Blittable):
Marshal.SizeOf<T>() - Managed objects: সঠিক সাইজ পাওয়া কঠিন, প্রোফাইলার ব্যবহার করুন
- Runtime-এ সাইজ: `Marshal.SizeOf` আনম্যানেজড রিপ্রেজেন্টেশনের সাইজ দেয়, ম্যানেজড হিপ সাইজ নয়