Logo

Programming-Idioms

Set n to the number of bytes of a variable t (of type T).
Implementation
C#

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another C# implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import "reflect"
var t T
tType := reflect.TypeOf(t)
n := tType.Size()
auto n = T.sizeof;
n := SizeOf(T);
N : Integer := (T'Size + 7) / 8;
import pympler.asizeof
n = pympler.asizeof.asizeof(t)
n = sizeof (t);
let n = ::std::mem::size_of::<T>();
#include <cstddef>
std::size_t n = sizeof(t);
use Devel::Size qw(total_size);
my $n = total_size $t;
use iso_c_binding
n = c_sizeof(i)
require 'objspace'
n = ObjectSpace.memsize_of(t)
int n;
unsafe
{
    n = sizeof(T);
}
using System.Runtime.InteropServices;
int n = Marshal.SizeOf(t);
String className = t.getClass().getSimpleName();
int n = 0;
if (className.equals("Byte")) {
	n = 1;
} else if (className.equals("Short")) {
	n = 2;
} else if (className.equals("Integer")) {
	n = 4;
} else if (className.equals("Long")) {
	n = 8;
} else if (className.equals("Float")) {
	n = 4;
} else if (className.equals("Double")) {
	n = 8;
} else if (className.equals("Boolean")) {
	n = 0;
} else if (className.equals("Character")) {
	n = 2;
}