COPYRIGHT
Cright 2019
All right rrvd. N part f this publication may b reproduced, ditributd, or trnmittd in n frm or b n mn, inluding hting, rrding, or thr ltrni r mhnil mthd, withut the prior written permission f the ublihr, xt in th case of brief uttin mbdid in ritil rviw nd certain thr nnmmril u permitted by copyright lw.
Table of Contents
INTRODUCTION
Every ftn in th rgrmming wrld, a new id i intrdud tht threatens to change vrthing. Although this is ftn a matter f hyperbole, th rlit i that th wrld f programming hng with dizzying d. If it i difficult fr rtiing programmers t t urrnt with th latest language developments, it might seem imibl fr beginning programmers to wrk with th ltt nd mt wrful languages.
Microsoft promises a groundbreaking dvlmnt with th intrdutin of the .NET architecture. Thi programming nvirnmnt lrl h the potential t b a mjr lr in th rgrmming univr. The .NET framework promises ll kind f thing tht advanced rgrmmr have bn clamoring fr, uh a imlifitin f th C++ syntax, an tu bjt mdl, nd integration f dtb int rgrmming lngug. Hwvr, th languages f th .NET frmwrk r nt nl fr dvnd rgrmmr. Mn of th innvtin f C# mk it n idl trting l fr bginning rgrmmr. C# is much fr and simpler t trt with thn mn f the thr vritin of C, nd it has a viul intrf nd powerful ditr tht rvid tn f help.
Th features tht mk C# a more advanced language often make it simpler t lrn, not more complex.
I will hw u m riu rgrmming, but youre going t hv a lot of fun lng th w. C# i a wrful, rfinl lngug, but lrning it doesnt have t b bring. Ill th u t rgrm th same w tht I lrndb writing gm.
Gm r a rtil, yet fun w t learn hw t rgrm, bu th r mtivting nd intrting. Gm l enable you t xlr some finting nt tht u dnt lw in thr frm f rgrmming.
Evn though you will b writing a lt f gm, Ill b ur t hw u a lt f mr riu rgrmming concepts lng the w. Youll lrn how h of th nt can b lid to rlwrld rgrmming.
Th best w t learn rgrmming i to writ rgrm. Yu huldnt simply rd thi bk; u huld also u ur computer. Look at the ur d frm th CDROM. Change th code rund. Kick the tir a littl bit. Tr the challenges I give u at the nd of h htr. U th examples t spark your intrt nd writ something ll ur wn.
If u d th thing, I rmi you tht b th nd f the bk, ull know a lot about th process of rgrmming. Youll l have a firm fundtin f th .NET frmwrk nd the C# lngug.
I hd a lot of fun writing this book, nd Im lking forward t hring frm u whn you ud, turn the g nd gt started!
CHAPTER ONE
BASIC INPUT AND OUTPUT: A MINI
Prgrmming i nt something u lrn b reading. Yu learn programming only b writing rgrm. In thi htr, u gt trtd b writing a iml (silly) adventure gm. You l gt the bi nt behind rgrmming in gnrl nd C# in rtiulr. In dditin t learning hw C# i rgnizd, you lrn hw to Writ th imlt intrf fr a C# nl rgrm.
Write data t th screen.
Gt information from the ur.
Crt bi vribl.
Project: Th Mini Adventure
Th game t the end of thi htr i iml but fun. Showing u the gm in rgr i easier thn dribing it, tk a lk at Figur 1.1 nd 1.2, whih hw th gm in rgr. Th mutr k th user a few utin nd thn mk a silly tr based on th ur rn.
Yu can that th game asks th user questions nd thn inrrt th nwr to rt a silly story. This gm rbbl wnt ll a millin i, but it uit imriv fr a firt program. After rding thi chapter, u will b bl t writ something like it.
Nt tht th user intrf i Srtnn flh graphics r eyecatching buttn and mnu. Fr nw, u are concentrating on th undrling nt. Th other lmnt will m n enough, but they add complications t ur life (which you dnt need jut t).
Rviwing Bi C# Cnt
Th C# lngug w designed t rfit from th xrin f thr rgrmming languages.
Th basic concepts behind C# rgrmming are rnt in vn the imlt rgrm.
Entill, a C# rgrm can b thught f an onion with a bunch of layers.
In the .NET nvirnmnt (f whih C# i a primary lngug) r layers f d that go frm gnrl to specific. The outer, mt gnrl, layer is the namespace. Inid a nm, you find a ri f classes, whih ntin methods, whih ntin ttmnt.
Tr Actually, thi i a imlifid viw. As u rgr thrugh thi book (and bnd), you will tht th .NET model ntin thr lmnt. Hwvr, thi reduced vrin f the mdl will uffi fr nw.
Nm
Th vriu layers f programming hl u rgniz ur programs. Even as a bginnr, u nd to undrtnd a littl bit but th various layers bu even th most rudimentary rgrm u them. Think f th layers mthing lik n address n n envelope. When you ddr n envelope, u write ifi information, uh as the hu number. Yu also ut th street nm, whih is more gnrl, nd th state, whih i brd. The t office n dlivr ur lttr by gtting it t th correct tt, thn the rrt city, then the right part of the it, and finll th specific hu. Namespaces in the C# language work vr muh lik thi.
Th lrgt lnd in th C# universe i a nm. You n think of a nm a tt in th postal nlg. A nm i n element tht enables u t group tgthr a ri f thr thing. Eh rjt you create i uull a nm. In addition, ll the various thing u can u in ur rgrminluding the mutr system itlf, nd Windows lmnt, uh as txt bx and buttonsare separated int namespaces. Fr untl, you specify whih nm you want to wrk with, fr example, t dfin whether a rgrm huld u Windows frm or a il librr f math functions. If all this m unclear t u, dnt worry but it.
Sn u will xml tht mk it lr.
Cl
A nm is usually md up of one r mr l. A l i a dfinitin fr a ifi kind f bjt. Thrughut th ntir bk, you will b learning about l and objects, but ntill, th r ud t drib m t f ntit.
Anthing a mutr n describe ( dtb, a fil, n image, a w, whatever) can be ndd an bjt. The thing n bjt n do are lld its mthd, nd the hrtriti f an bjt are lld it rrti. Dnt wrr, there int a quiz n ll thi thr. Yu d need an intrdutin t these concepts, thugh, bu ll f C# i bd n the idea of objects.
Mthd
Cl always hv methods. A method i a trutur that ntin instructions. All th commands in a program r hud in vriu mthd f objects. Mt programs have a special method nmd Min() (mthd nm lw end in parentheses), which i mnt to execute n as th program bgin running. If you r fmilir with thr lngug, such C or Viul Bi, u will tht mthd are a lot like functions or ubrgrm in those languages.
Statements
Inid a mthd, u write the intrutin u wnt the mutr t execute. A statement is n instruction. Mn ttmnt (mtim l lld mmnd) involve uing mthd f builtin bjt. Of ur, a mutr intit wuldnt uull using a mthd, because vrn wuld undrtnd that. Often C# folks will rfr t th process invoking a mthd. Mb t dinnr tnight rthr thn king mbd to the salt, you uld Culd u please invk the lt hkr bjt pass mthd? It huld liven up th conversation. Other commands are built in t th structure f the lngug.
Trik Dnt wrr if ll this talk but methods nd namespaces i mking u dizz. You dnt have to memorize ll this now, but u will b using it later. Evn the imlt rgrm uses all these lvl of intrutin, so u nd to have m id f these trm. However, you probably wont fully undrtnd them until u build a fw utm nm, l, nd methods dwn th road. Evrbd nd tim in nfuin until th lrgr picture bm lr.
The Cnl Objt
To see hw ll thi wrk, take a look t one specific bjt, th nl. In the bd old days f computing before viul intrf lik Windw, ll intrtin with a mutr was dn thrugh a lin txt rn. Th mbintin of the txt screen nd th kbrd i uull rfrrd t the nl. Although rgrmming on th nl might m kind f ldfhind, it a gd l t trt bu rgrm whih ftur th nl r ir t write thn th fancier rgrm uing Windows frm. In C#, vrthing i an object, ull wrk with th nl by wrking with a il object, l lld the Cnl. Nt tht the nm f classes are capitalized, whn Im rfrring t th tul Console, l, I u a capital C. Most f ur rl rgrm will be built using the Cnl bjt, tking a lk at how C# sees thi bjt i a gd id.