Home

The sizeof Operator

Introduction

An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and at least one value. The symbol used in an operation is called an operator. A value involved in an operation is called an operand.

A unary operator is an operator that performs its operation on only one operand. An operator is referred to as binary if it operates on two operands.

Unary Operators: The sizeof Operator

When declaring a variable, the compiler reserves a portion of space in the computer memory to hold that variable. We also illustrated how much space some data types need to store their variable. C++ provides the unary sizeof operator that allows you to find out how much space a data type. You can use the sizeof operator as unsafe in your C# application.

To find out how much memory space a data type uses, type it between the parentheses of the sizeof operator. Remember that sizeof is unsafe. Therefore, a method that uses it must be preceded with the unsafe keyword. Here is an example:

using System;

class Exercise
{
	unsafe static void Main()
	{
		double Period = 155.50;
		int SizeOf = sizeof(double);

		Console.Write("The value ");
		Console.Write(Period);
		Console.Write(" uses ");
		Console.Write(SizeOf);
		Console.WriteLine(" bytes\n");
	}
}

This would produce:

The value 155.5 uses 8 bytes

 

 

Home Copyright © 2007-2013, FunctionX