Data Types: Type Description Size (Bytes) Range
Data Types: Type Description Size (Bytes) Range
All applications store and manipulate data within the computer's memory. C#
supports two kinds of data types used to represent real-world information. Value
types are so-called because they contain the actual value of the data they store. For
example, you might have an int type that stores the value 3. The literal value of 3 is
stored in the variable that you declare to hold it.
With the exception of DateTime and string, in the below table, the data types listed
are aliases for structs in .NET that represent the data types in the Microsoft .NET
Framework. Anyplace you can use int you can also use System.Int32. We'll cover
structs in module four.
Reference types are also known as objects. Reference types are created from class
files, which you will cover in module five A reference type stores a reference to the
location in memory of the object. . If you are familiar with C/C++ then you can
think of a reference to the memory location to be the same as a pointer. C# does
not require you to use pointers.
The following table shows the most commonly used value types.
Type
Description
int
Whole numbers
long
float
double
decima
l
char
bool
DateTi
me
string
Size
(bytes)
4
.NET Type
Range
System.Int32
Whole numbers
(bigger range)
System.Int64
Floating-point
numbers
Double precision
(more accurate)
floating-point
numbers
Monetary values
System.Singl
e
System.Doubl
e
-2,147,483,648 to
2,147,483,647
9,223,372,036,854,775,80
8 to
9,223,372,036,854,775,80
7
+/-3.4 x 10^38
Single character
Boolean
2
1
Moments in time
Sequence of
characters
2 per
charact
er
16
System.Deci
mal
System.Char
System.Boole
an
System.DateT
ime
System.String
+/-1.7 x 10^308
28 significant figures
N/A
True or false
0:00:00 on 01/01/0001 to
23:59:59 on 12/31/9999
N/A
Statements
In C#, a statement is considered a command. Statements perform some action in
your code such as calling a method or performing calculations. Statements are also
used to declare variables and assign values to them.
Statements are formed from tokens. These tokens can be keywords, identifiers
(variables), operators, and the statement terminator which is the semicolon (;). All
statements in C# must be terminated with a semicolon.
Identifiers
In C#, an Identifier is name you give to the elements in your program. Elements in
your program include;
Classes - classes are the blueprints for reference types. They specify the
structure an object will take when you create instances of the class
Variables - these are identifiers that you create to hold values or references
to objects in your code. A variable is essentially a named memory location
When you create a variable in C# you must give it a data type. You can assign a
value to the variable at the time you create it or later in your program code. C# will
not allow you to use an unassigned variable to help prevent unwanted data from
being used in your application. The following code sample demonstrates declaring a
variable and assigning a value to it.
int myVar = 0;
C# has some restrictions around identifiers that you need to be aware of.
First off, identifiers are case-sensitive because C# is a case-sensitive language.
That means that identifiers such as myVar, _myVar, and myvar, are considered
different identifiers.
Identifiers can only contain letters (in any case), digits, and the underscore
character. You can only start an identifier with a letter or an underscore character.
You cannot start the identifier with a digit. myVar and _myVar are legal but 2Vars
is not.
C# has a set of reserved keywords that the language uses. You cannot use these
keywords as an identifier in your code. You may choose to take advantage of the
case-sensitivity of C# and use Double as an identifier to distinguish it from the
reserved keyword double, but that is not a recommended approach.
The following table contains the C# reserved keywords.
abstract
as
base
bool break
byte
case
catch char
checked
class
const continue
decimal
default
delegate do
double
else
enum event
explicit
extern
false finally
fixed
float
for foreach
goto
if
implicit in
in (generic
modifier)
int
interface internal
is
lock
long namespace
new
null
object operator
out
out (generic
modifier)
override params
private
protected
public readonly
ref
return
sbyte sealed
short
sizeof
stackalloc static
string
struct
switch this
throw
true
try typeof
uint
ulong
unchecked unsafe
ushort
using
virtual void
volatile
while
Operators
When writing C# code, you will often use operators. An operator is a token that
applies to operations on one or more operands in an expression. An expression can
be part of a statement, or the entire statement. Examples include:
3 + 4 an expression that will result in the literal value 4 being added to the literal
value 3
counter++ an expression that will result in the variable (counter) being
incremented by one
Not all operators are appropriate for all data types in C#. As an example, in the
preceding list the + operator was used to sum two numbers. You can use the same
operator to combine two strings into one such as:
Tom + Sawyer which will result in a new string TomSawyer
You cannot use the increment operator (++) on strings however. In other words,
the following example would cause an error in C#.
Tom++
The following table lists the C# operators by type.
Type
Operators
Arithmetic
+, -, *, /, %
Increment, decrement
++, --
Comparison
String concatenation
Logical/bitwise operations
&, |, ^, !, ~, &&, ||
[]
Casting
( ), as
Assignment
Bit shift
<<, >>
Type information
sizeof, typeof
+, -
checked, unchecked
*, ->, [ ], &
?:
Data Conversions
C# supports two inherent types of conversion (casting) for data types, implicit and
explicit. C# will use implicit conversion where it can, mostly in the case when a
conversion will not result in a loss of data or when the conversion is possible with a
compatible data type. The following is an example of an implicit data conversion.
Converting from smaller to larger integral types: