03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.2 Types<br />

The language C# is strongly typed. This means that a compile time error occurs if<br />

we make a type error.<br />

Some basic types are:<br />

Type Example Notes<br />

int ‐100 32‐bit<br />

char ‘x’ Uni‐code<br />

string “Andrew was here.” Strings are double quoted.<br />

Object new String(‘a’,10) Object is the most general type of object.<br />

Variables are declared by giving the type followed by the name of the variable. In<br />

these examples the variables are declared and given an initial value in the same line.<br />

Sample program fragment<br />

char c = 'x';<br />

string s = "Andrew was here.";<br />

int i = -100;<br />

double d = 3.1415;<br />

Object s = new System.IO.StreamReader("file.txt");<br />

Object h = new System.Collections.Hashtable();<br />

.<br />

2.3 Type Casting<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

A type cast can be used <strong>to</strong> convert one type <strong>to</strong> another.<br />

The cast may be either explicit of implicit.<br />

Sample program fragment<br />

char c0 = 'A';<br />

int i0 = c0; // Implicit cast works<br />

//string s0 = c; // This fails<br />

//string s1 = (string) c; // This fails<br />

string s2 = "A" + c0; // This succeeds<br />

double d0 = i0;<br />

float f0 = (float)d0;<br />

//float f1 = d0; // This fails<br />

int i1 = (int)d0;<br />

//int i1 = d0; // This fails<br />

int i2 = (int)c0;<br />

Agilent .NET Course: C# Basics 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!