Sizeof()
The sizeof Operator is used to Obtain the Size in bytes of a Given type: In order to use this operator, we should keep two extremely important factors in our mind.
Using sizeof():
In order to use the sizeof() Operator in C#, we need to declare our Methods as unsafe using Unsafe Keyword. and then we can use this sizeof() operator inside the body of that unsafe method, the syntax is:
using System;
namespace size_OF
{
class Program
{
static unsafe void Main(string[] args) //unsafe method:
{
sizeof(type); /*type could be any user define type
like struct: or built-in type: like int or float:*/
}
}
}
Instead of declaring an entire method as unsafe, we can also declare a part of the code as unsafe and use the sizeof() operator inside this unsafe part, and use/call it in safe method of the code:
using System;
namespace size_OF
{
class Program
{
public unsafe void show()//unsafe method:
{
sizeof(type);
}
static void Main(string[] args) //safe method:
{
Program pro = new Program(); //creating object:
pro.show(); //calling method:
}
}
}
Example:
Let's have an simple example to know how can we get the size of any type using the sizeof() operator in C#. in this example we will try to get the size of the some built-in data types:
/*Example: Sizeof(): InfoBrother:*/
using System;
namespace size_OF
{
class Program
{
public unsafe void show()//unsafe method:
{
//sizeof(type);
Console.WriteLine("Size of int: " + sizeof(int));
Console.WriteLine("Size of float: " + sizeof(float));
Console.WriteLine("Size of double: " + sizeof(double));
Console.WriteLine("Size of Decimal: " + sizeof(decimal));
Console.WriteLine("Size of Char: " + sizeof(char));
Console.WriteLine("Size of bool: " + sizeof(bool));
Console.WriteLine("Size of Long: " + sizeof(long));
}
static void Main(string[] args) //safe method:
{
Program pro = new Program(); //creating object:
pro.show(); //calling method:
Console.ReadKey();
}
}
}
Sizeof(Built-in Datatype):
Size of int : 4 Size of float : 4 Size of double : 8 Size of Decimal: 16 Size of Char : 2 Size of bool : 1 Size of Long : 8
Aside from simple build-in types, The sizeof Operator can also be used to determine the sizes of other user-defined data types such as Struct: However, the results of the sizeof Operator can sometimes be less than obvious, such as in the following example:
/*Example: Sizeof(): InfoBrother:*/
using System;
namespace size_OF
{
class Program
{
struct withoutMembers
{
//structure having no members:
};
struct withMembers
{
//structure having some members:
int a;
float b;
char c;
double d;
}
struct composite
{
//Structure that aggregates the Both:
withMembers a;
withoutMembers b;
}
static unsafe void Main(string[] args)
{
Console.WriteLine("Sizeof struct without Members: "
+ sizeof(withoutMembers));
Console.WriteLine("Sizeof struct with Members: "
+ sizeof(withMembers));
Console.WriteLine("Sizeof Composite Struct: "
+ sizeof(composite));
Console.ReadKey();
}
}
}
Sizeof(User-defined datatype):
Sizeof struct without Members: 1
Sizeof struct with Members: 24
Sizeof Composite Struct: 32
While you might expect this application to print a value of 0 for the structure with no members withoutMembers , the value 23 for the structure with four of the basic members withMembers , and the value 23 for the structure that aggregates the two ( composite , but the actual result Is different:
The reason for this is padding and structure alignment, which relates to how the compiler lays out a struct in the output file's image. For example, if a struct were three bytes long and the byte alignment were set at four bytes, the compiler would automatically pad it with an extra byte and the sizeof operator would report that the struct is four bytes in length. Therefore, you must take this into consideration when determining the size of a structure in C#.