Saturday, 17 September 2011

Data Types in C Sharp


This post contains a list of the C# language data types, their working properties and a few notes on variables.

C# is a statically typed language. That means that every variable and constant must have a type specified when the variable is declared to the compiler.

Furthermore, C# is a strongly typed language. This means that there are hard rules about what types your data is allowed to be and hard rules about how the compiler will interpret that data and allow you to use it.

Making the language have hard rules on data types is done for security purposes. The compiler will stop you from trying to operate on one type of data if it is expecting to see another (unless you have explicitly written a piece of code to explain to the compiler how to do the thing you are asking for). For example a routine to add together two integers will not be allowed to run over two linked-lists -- unless you have written code to explain to the compiler how to add together two linked-lists. It is all about data security and validity.

In C# we need to explicitly tell the compiler which type each variable is going to be before we user it. But we have the ability to explicitly 'cast' arithmetic values from one type to another within the program if the need arises.

There are a number of basic data types available for use in the C# language. These types are listed and explained below.

Integer C# Data Types:

Quick rules:
  • Integers are whole numbers -- they are not allowed to have fractional parts.
  • Remember that integers have no default value. You need to ensure that the variable is intialized correctly to guarantee expected algorithmic behaviour.
  • If, for some reason, you are trying to put a fractional number into an integer then the C# compiler will round down (throw away, truncate) the fractional component.
    e.g. int a = 4.989; will be considered by the compiler as a = 4

The Integer Types:

Type Name Bits Range Notes
byte 8 0 to 255
sbyte 8 –128 to 127
short 16 –32,768 to 32,767
ushort 16 0 to 65,535
int 32 –2,147,483,648 to 2,147,483,647
uint 32 0 to 4,294,967,295 Append literals with U or u
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Append literals with L or l
ulong 64 0 to 18,446,744,073,709,551,615 Append literals with UL or ul


Bool Data Type in C#:

Quick Rules:
  • An uninitialized boolean will always be set to false by the compiler

The Boolean Type:

Type Name Bits Range Notes
bool 8 false or true Initial value is set to false


Floating-Point C# Data Types:

Quick Rules:
  • An uninitialized floating-point number will be set to 0.0 by the compiler

The Fractional Types:

Type Name Bits Range Notes
float 32 ±1.401298E−45 to ±3.402823E+38 Append literals with F or f
double 64 ±4.94065645841246E−324 to ±1.79769313486232E+308 Append literals with D or d
decimal 128 −7.9228162514264337593543950335 to +7.9228162514264337593543950335 Append literals with M or m


Char Data Type in C#:

Quick Rules:
  • This is a single unicode character NOT an 8-bit ASCII character
  • An uninitialized char will be initialized to "\u0000" by the compiler

The Char Type:

Type Name Bits Range Notes
char 16 "\u0000" to "\uFFFF" Initial value is set to "\u0000"

Variables in C#:

Quick Rules:
  • Simple variable names should be in camel case e.g.:
  • Variable names may:
    - start with an underscore "_"
    - contain unicode characters

  • Variable names may not:
    - be longer than 511 characters
    - start with a numeral
    - start with a symbol (there is an exception to this for keywords, but just assume it is a rule for now)

  • A variable can be declared and later initialized:
  • A variable can be declared and initialized on one line:
  • Multiple declarations of variables of the same type can be separated by a comma:
  • When a variable is declared it binds an object to an identifier

  • When instances of value types are bound to an identifier they reside on the stack

  • Binding an object to an identifier allocates memory directly and the object is destroyed when it goes out of scope


References:

[1] Wikipedia -- Strong Typing
[2] Wikipedia -- C# Syntax (Types)
[3] Wikipedia -- Unicode characters
[4] .NET Naming Conventions Best Practice
[5] MSDN StyleCop Enforces Consistency Rules

No comments:

Post a Comment