What are unary operators in C#?



The following are the unary operators in C# βˆ’

+ - ! ~ ++ -- (type)* & sizeof

Let us learn about the sizeof operator. The sizeof returns the size of a data type.

Let’s say you need to find the size of int datatype βˆ’

sizeof(int)

For double datatype βˆ’

sizeof(double)

Let us see the complete example to find the size of various datatypes βˆ’

Example

 Live Demo

using System;

namespace Demo {

   class Program {

      static void Main(string[] args) {

         Console.WriteLine("The size of int is {0}", sizeof(int));
         Console.WriteLine("The size of int is {0}", sizeof(char));
         Console.WriteLine("The size of short is {0}", sizeof(short));
         Console.WriteLine("The size of long is {0}", sizeof(long));
         Console.WriteLine("The size of double is {0}", sizeof(double));

         Console.ReadLine();
      }
   }
}

Output

The size of int is 4
The size of int is 2
The size of short is 2
The size of long is 8
The size of double is 8
Updated on: 2020-06-20T13:17:19+05:30

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements