Geekpedia Programming Tutorials






C# data types

Quickly covers C#'s data types by giving a few examples. Good for transition between C or C++ to C#.

On Saturday, April 3rd 2004 at 05:50 PM
By Andrew Pociu (View Profile)
***--   (Rated 3.1 with 16 votes)
Contextual Ads
More C# Resources
Advertisement

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).
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by wrong on Monday, November 21st 2005 at 02:39 PM

float is actually 8 bits exponent, 23 bits mantissa, 1 sign bit

by dileep S on Wednesday, September 13th 2006 at 01:09 AM


There is any maximum limit for string . if so what is that ?

by praveenkumarpathak on Friday, December 14th 2007 at 06:50 AM

please program my hand over me.

praveen kumar pathak

by Corey Ogburn on Monday, May 19th 2008 at 04:39 PM

This subject has been thrown around a little at the office here recently. We agreed that all arrays have one limit, the index has to be an integer. Therefore, the array can't be bigger then an integer value can hold ( 2147483647). So roughly 2 GBs. We agree even more so that you'll run out of RAM long before you'll run out of array length. ha ha


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons