Home  >  Article  >  Backend Development  >  What are unary operators in C#?

What are unary operators in C#?

PHPz
PHPzforward
2023-09-07 10:05:021393browse

C# 中什么是一元运算符?

The following are the unary operators in C# -

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

Let us understand the sizeof operator. sizeof returns the size of the data type.

Suppose you need to find the size of int data type -

sizeof(int)

For double data type -

sizeof(double)

Let’s see the complete example to find the size of various data types -

Example

Real-time demonstration

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

The above is the detailed content of What are unary operators in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete