sizeof() Operator in C# with Example

C# sizeof() Operator: Here, we will find out about the sizeof() administrator with example in C#.

C# sizeof() Operator

sizeof() is an administrator in C#, it is utilized to get the size in bytes of arranging time known sorts, it doesn’t work with the variables or examples.

Syntax:

    int sizeof(type);

It acknowledges the sort and returns int esteem – which is the size of that type in bytes.

Example:

    sizeof(char)    - 2
    sizeof(int)     - 4
    sizeof(long)    - 8

Consider the beneath code – indicating the size of various kinds.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("size of {0} is {1} bytes", typeof(bool), sizeof(bool));
            Console.WriteLine("size of {0} is {1} bytes", typeof(byte), sizeof(byte));
            Console.WriteLine("size of {0} is {1} bytes", typeof(char), sizeof(char));
            Console.WriteLine("size of {0} is {1} bytes", typeof(UInt32), sizeof(UInt32));
            Console.WriteLine("size of {0} is {1} bytes", typeof(ulong), sizeof(ulong));
            Console.WriteLine("size of {0} is {1} bytes", typeof(decimal), sizeof(decimal));            

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

size of System.Boolean is 1 bytes
size of System.Byte is 1 bytes
size of System.Char is 2 bytes
size of System.UInt32 is 4 bytes
size of System.UInt64 is 8 bytes
size of System.Decimal is 16 bytes

Leave a Comment

error: Alert: Content is protected!!