---
title: "How to get the sizeof() a datatype in C#?"  
description: "How to get the sizeof() a datatype in C#?"  
author: "Ravi Vishwakarma"  
published: 2024-06-07  
updated: 2024-06-07  
canonical: https://www.mindstick.com/interview/33893/how-to-get-the-sizeof-a-datatype-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# How to get the sizeof() a datatype in C#?

In C#, you could determine the scale of a cost kind the usage of the **sizeof the operator** or the **Marshal.SizeOf technique** from the **System.Runtime.InteropServices namespace**. However, the **sizeof** the operator can only be used with **unmanaged sorts** and inside a **risky** context. For managed kinds or preferred utilization, **Marshal.SizeOf** is more suitable.

## Here are examples of each strategy:

**Using sizeof Operator**\
The sizeof the operator is used in a hazardous context to get the scale of unmanaged kinds. Unmanaged types include primitive information sorts (like int, flow, etc.) and person-described structs that don't contain any reference sorts.

```cs
using System;
namespace ConsoleApplication1
{
    public class CheckSizeof
    {
        unsafe public static void Main()
        {
            // Example of using sizeof operator
            Console.WriteLine("Size of int: {0} bytes", sizeof(int));
            Console.WriteLine("Size of double: {0} bytes", sizeof(double));
            Console.WriteLine("Size of char: {0} bytes", sizeof(char));
            Console.WriteLine("Size of DemoStruct: {0} bytes", sizeof(DemoStruct));
            Console.ReadLine();
        }

        //Create a DemoStruct of struct datatype
        struct DemoStruct
        {
            char ch;
            int x;
            float y;
            double z;
        }
    }
}
```

**Note:** To use the sizeof operator, you need to assemble with the /hazardous option and mark your code with the **risky keyword**.

**Using Marshal.SizeOf Method**\
The Marshal.SizeOf approach can be used with both managed and unmanaged types and does no longer require an unsafe context.

```cs
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public class CheckSizeof
    {
        static void Main()
        {
            // Example of using Marshal.SizeOf method
            Console.WriteLine("Size of int: {0} bytes", Marshal.SizeOf(typeof(int)));
            Console.WriteLine("Size of double: {0} bytes", Marshal.SizeOf(typeof(double)));
            Console.WriteLine("Size of char: {0} bytes", Marshal.SizeOf(typeof(char)));

            // Example with a custom struct
            Console.WriteLine("Size of MyStruct: {0} bytes", Marshal.SizeOf(typeof(MyStruct)));
            Console.ReadLine();
        }

        struct MyStruct
        {
            public int x;
            public double y;
            public char z;
        }
    }
}
```

**Explanation:**\
**Using sizeof:**

1. Only works with primitive types and calls for risky code.
2. Provides the dimensions in bytes of the specified type.

## Using Marshal.SizeOf:

1. Works with both controlled and unmanaged sorts.
2. Does no longer requires risky code.
3. The Marshal.SizeOf technique returns the size in bytes of the specified type.

Choose the approach that first-rate suits your necessities and the context in which you're running. For maximum fashionable functions, **Marshal.SizeOf** is more versatile and less difficult to apply because it does now not require an **unsafe context**.

## Answers

### Answer by Ravi Vishwakarma

In C#, you could determine the scale of a cost kind the usage of the **sizeof the operator** or the **Marshal.SizeOf technique** from the **System.Runtime.InteropServices namespace**. However, the **sizeof** the operator can only be used with **unmanaged sorts** and inside a **risky** context. For managed kinds or preferred utilization, **Marshal.SizeOf** is more suitable.

## Here are examples of each strategy:

**Using sizeof Operator**\
The sizeof the operator is used in a hazardous context to get the scale of unmanaged kinds. Unmanaged types include primitive information sorts (like int, flow, etc.) and person-described structs that don't contain any reference sorts.

```cs
using System;
namespace ConsoleApplication1
{
    public class CheckSizeof
    {
        unsafe public static void Main()
        {
            // Example of using sizeof operator
            Console.WriteLine("Size of int: {0} bytes", sizeof(int));
            Console.WriteLine("Size of double: {0} bytes", sizeof(double));
            Console.WriteLine("Size of char: {0} bytes", sizeof(char));
            Console.WriteLine("Size of DemoStruct: {0} bytes", sizeof(DemoStruct));
            Console.ReadLine();
        }

        //Create a DemoStruct of struct datatype
        struct DemoStruct
        {
            char ch;
            int x;
            float y;
            double z;
        }
    }
}
```

**Note:** To use the sizeof operator, you need to assemble with the /hazardous option and mark your code with the **risky keyword**.

**Using Marshal.SizeOf Method**\
The Marshal.SizeOf approach can be used with both managed and unmanaged types and does no longer require an unsafe context.

```cs
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public class CheckSizeof
    {
        static void Main()
        {
            // Example of using Marshal.SizeOf method
            Console.WriteLine("Size of int: {0} bytes", Marshal.SizeOf(typeof(int)));
            Console.WriteLine("Size of double: {0} bytes", Marshal.SizeOf(typeof(double)));
            Console.WriteLine("Size of char: {0} bytes", Marshal.SizeOf(typeof(char)));

            // Example with a custom struct
            Console.WriteLine("Size of MyStruct: {0} bytes", Marshal.SizeOf(typeof(MyStruct)));
            Console.ReadLine();
        }

        struct MyStruct
        {
            public int x;
            public double y;
            public char z;
        }
    }
}
```

**Explanation:**\
**Using sizeof:**

1. Only works with primitive types and calls for risky code.
2. Provides the dimensions in bytes of the specified type.

## Using Marshal.SizeOf:

1. Works with both controlled and unmanaged sorts.
2. Does no longer requires risky code.
3. The Marshal.SizeOf technique returns the size in bytes of the specified type.

Choose the approach that first-rate suits your necessities and the context in which you're running. For maximum fashionable functions, **Marshal.SizeOf** is more versatile and less difficult to apply because it does now not require an **unsafe context**.


---

Original Source: https://www.mindstick.com/interview/33893/how-to-get-the-sizeof-a-datatype-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
