1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Tutorial - Getting Started

By , About.com Guide

5 of 8

C# is a Strongly Typed Language

When we define variables to hold data we have to specify the type of data that those variables will hold. The compiler then checks that what we are doing with the data makes sense to it, i.e. follows the rules. We can't for example store text in a number - the compiler will not allow it.
int a = "fred"; // Not allowed. Cannot implicitly convert 'string' to 'int'
The variable a is of type int, and assigning it the value "fred" which is a text string breaks the rules- the compiler is unable to do any kind of conversion of this string. Some languages like PHP will allow this but it makes the programmers life easier if they don't. This is correct.
int a = 986;
This is called an initialization statement. The word int is the type of the variable, a is the name of the variable and 986 is its initial value. The size of int varies from computer to computer but on most pcs it is 32 bits. It can hold negative numbers as well as positive. The range of values that can be stored in an int is -2,147,483,648 to 2,147,483,647. This is called a signed 32 bits integer.

Here is a full list of the integer types you can use.

  • byte : 0 to 255 - Unsigned 8-bit integer
  • char : U+0000 to U+ffff Unicode 16-bit character
  • short : -32,768 to 32,767 Signed 16-bit integer
  • ushort : 0 to 65,535 Unsigned 16-bit integer
  • int : -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
  • uint : 0 to 4,294,967,295 Unsigned 32-bit integer
  • long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer
  • ulong : 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer

On the next page : An Example of Counting

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. C# Is a Strongly Typed Language

©2009 About.com, a part of The New York Times Company.

All rights reserved.