sizeof() Operator in C#

The sizeof() operator is used to obtain the size of a data type in bytes in bytes. It will not return the size of the variables or instances. Its return type is always int.

Syntax:

int sizeof(type);

Examples:

Input : sizeof(byte);
Output : 1

Input : sizeof(int);
Output : 4
filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// sizeof() operator
using System;
using System.IO;
using System.Text;
   
namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            Console.WriteLine("sizeof(char)     : {0}", sizeof(char));
            Console.WriteLine("sizeof(byte)     : {0}", sizeof(byte));
            Console.WriteLine("sizeof(sbyte)    : {0}", sizeof(sbyte));
            Console.WriteLine("sizeof(float)    : {0}", sizeof(float));
            Console.WriteLine("sizeof(ushort)   : {0}", sizeof(ushort));
            Console.WriteLine("sizeof(double)   : {0}", sizeof(double));
            Console.WriteLine("sizeof(int)      : {0}", sizeof(int));
            Console.WriteLine("sizeof(bool)     : {0}", sizeof(bool));
            Console.WriteLine("sizeof(short)    : {0}", sizeof(short));
               
        }
    }
}

chevron_right


Output:

sizeof(char)     : 2
sizeof(byte)     : 1
sizeof(sbyte)    : 1
sizeof(float)    : 4
sizeof(ushort)   : 2
sizeof(double)   : 8
sizeof(int)      : 4
sizeof(bool)     : 1
sizeof(short)    : 2
My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.