Quickly covers C#'s data types by giving a few examples. Good for transition between C or C++ to C#.
C# Data Types
The following example will show you the information you need about C#’s data types:
// DataTypes.cs
using System;
class DataTypes
{
static void Main()
{
Console.WriteLine("The minimum and maximum sizes of C#'s datatypes:\n");
Console.WriteLine("sbyte\nMin: {0}\nMax: {1}\n", sbyte.MinValue, sbyte.MaxValue);
Console.WriteLine("byte\nMin: {0}\nMax: {1}\n", byte.MinValue, byte.MaxValue);
Console.WriteLine("short\nMin: {0}\nMax: {1}\n", short.MinValue, short.MaxValue);
Console.WriteLine("ushort\nMin: {0}\nMax: {1}\n", ushort.MinValue, ushort.MaxValue);
Console.WriteLine("int\nMin: {0}\nMax: {1}\n", int.MinValue, int.MaxValue);
Console.WriteLine("uint\nMin: {0}\nMax: {1}\n", uint.MinValue, uint.MaxValue);
Console.WriteLine("long\nMin: {0}\nMax: {1}\n", long.MinValue, long.MaxValue);
Console.WriteLine("ulong\nMin: {0}\nMax: {1}\n", ulong.MinValue, ulong.MaxValue);
Console.WriteLine("float\nMin: {0}\nMax: {1}\n", float.MinValue, float.MaxValue);
Console.WriteLine("double\nMin: {0}\nMax: {1}\n", double.MinValue, double.MaxValue);
Console.WriteLine("decimal\nMin: {0}\nMax: {1}\n", decimal.MinValue, decimal.MaxValue);
Console.ReadLine(); // Keep the console opened until you press Enter
}
}
The output of the program is:
The minimum and maximum sizes of C#'s datatypes:
sbyte
Min: -128
Max: 127
byte
Min: 0
Max: 255
short
Min: -32768
Max: 32767
ushort
Min: 0
Max: 65535
int
Min: -2147483648
Max: 2147483647
uint
Min: 0
Max: 4294967295
long
Min: -9223372036854775808
Max: 9223372036854775807
ulong
Min: 0
Max: 18446744073709551615
float
Min: -3.402823E+38
Max: 3.402823E+38
double
Min: -1.79769313486232E+308
Max: 1.79769313486232E+308
decimal
Min: -79228162514264337593543950335
Max: 79228162514264337593543950335
sbyte is the signed version of byte (8 bits);
short is the signed version of ushort (16 bits);
int is the signed version of uint (32 bits);
long is the signed version of ulong (64 bits);
The ‘char’ type stores a single character;
The ‘string’ data type stores multiple characters;
Getting the length of a string
Suppose you have the ‘content’ variable that is a ‘string’ type of variable:
string content = "The actual string";
You can find the length of this string using:
content.Length
For example:
using System;
class StringLength
{
static void Main()
{
string Content = "The actual string";
Console.WriteLine("{0}\n", Content);
Console.WriteLine("The length of the above string is {0} characters", Content.Length);
Console.ReadLine(); // Keep the console opened until you press Enter
}
}
The ‘float’ data type is 32 bits long. It has an 8 bits long exponent and 24 bits long mantissa;
The ‘double’ data type is 64 bits long. It has an 11 bits long exponent and 53 bits long mantissa;
C# arrays
You can initialize an array using:
int[] objects;
If you want to allocate a space in memory for the elements of the array use:
int[] objects = new int[5];
or separate:
int[] objects;
objects = new int[5];
This is a complete example of initializing an array and accessing it using a loop:
using System;
class Arrays
{
static void Main()
{
char[] website = new char[9] {'G', 'e', 'e', 'k', 'p', 'e', 'd', 'i', 'a'};
int i = 0;
for (i = 0; i < website.Length; i++)
{
Console.Write("{0}", website[i]);
}
Console.ReadLine();
}
}
(The above example could be simplified by using a ‘foreach’ loop like in VB but I wanted to stick to the topic).