Home » C#.Net

sizeof() Operator in C# with Example

C# sizeof() Operator: Here, we are going to learn about the sizeof() operator with example in C#.
Submitted by IncludeHelp, on February 13, 2019

C# sizeof() Operator

sizeof() is an operator in C#, it is used to get the size in bytes of compile-time known types, it does not work with the variables or instances.

Syntax:

    int sizeof(type);

It accepts the type and returns an int value – which is the size of that type in bytes.

Example:

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

Consider the below code – showing the size of different types.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.