Boo Succinctly Revealed

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Boo Succinctly Revealed (second edition)

By Rolly Noel, Copyright 2013


[THIS !C"#$NT !$SN%T C!&$R 'N(THIN) TH'T N$$S 'N I#*!RT (e+cept ,or Syste-).
[/IRST $/INITI!N !/ B!! 0'N)"')$ IT$#S IS IN B!0.
[IN THIS !C"#$NT, 0IN$S !/ $SCRI*TI!N (N!T C!$) ST'RT 1ITH 232, 'N 'R$ C!NTIN"$ 1ITH 2 2
print %' Co-plete Boo progr4-% 3!5*s (o6tp6ts) %' Co-plete Boo progr4-%
3!rder o, p4rts (4ll p4rts option4l)7 222-od6le docstring222, n4-esp4ce, i-ports, en6-s5str6ct6res5cl4sses5,6nctions, -4in code,
4sse-8ly 4ttri86tes
3I, yo6 h49e 4 2de, #4in (4rg9 4s (string))72, e+ec6tion 8egins therein, else it st4rts 4t 6nenclosed lines o, code (not in 4 cl4ss,
,6nction, str6ct6re, :::)
Definitions
3Comments to end o, line (4lso //)
/* Co--ents (5; c4n 8e nested ;5) (c4n 8e -6lti<line) */
""" docstring co--ents, on the line 8elo= =h4t%s 8eing doc6-ented, si-il4rly indented 222
3#6ltiple Boo st4te-ents on the s4-e line 4re sep4r4ted 8y 2>2 (c4n%t do =ithin 4 co--ent)
3To contin6e 4 Boo st4te-ent 4ny=here on the ne+t line, end the contin6ed line =ith 4 ?
namespace *ro@ectN4-e 3Set o, o8@ects cont4ined herein =ill 8e 6niA6ely pre,i+ed =ith *ro@ectN4-e (-6st 8e 4t top o, ,ile)
import Syste-:Console 3#4Bes 4ll -e-8ers o, the n4-esp4ce 494il48le: C4n s4y Re4d0ine() inste4d o,
Syste-:Console:Re4d0ine()
i-port Syste- 4s S 3c4n s4y S:Console:Re4d0ine()
i-port )tB from 2gtB<sh4rp2 3"sed =hen 4sse-8ly n4-e di,,ers ,ro- n4-esp4ce n4-e
3In the Sh4rpe9elop I$ (#D (494il48le ,or 1indo=s ,ro- http755===:icsh4rpcode:net5!penSo6rce5S5o=nlo4d5 )),
typing i-port then 4 sp4ce, lists 4ll the n4-esp4ces yo6 c4n i-port (=ith s6pple-ent4ry in,o, i, 494il48le,
,or the highlighted entry)
3 3 4lso does this ,or Boo Bey=ords: It =ill 4nticip4te yo6 =hen yo6 st4rt typing so-ething it recogniCes
3 3 4lso lists yo6r options (4long =ith their prototypes D 6s4ge in,o) =hen yo6 type 4 2:2 or 2(2 i--edi4tely ,ollo=ing the
n4-e o, 86ilt<in or 6ser de,ined types < t48 to enter the highlighted ite-
3Variable n4-es 8egin =ith 4n 4lph4 ch4r4cter or 4n E, cont4in 4lph4n6-erics D E, 4re c4se sensiti9e, D c4n%t 8e reser9ed =ords
3&alue type 94ri48les cont4in the 94l6e itsel,
3Reference type 94ri48les (they de,46lt to null) point to 4n o8@ect (4 string, or :::)
+ as duck 3Co-piler =ill not do type checBing ,or 6ses o, this 94ri48le: Slo=s do=n code e+ec6tion 86t 6se,6l =ith e+tern4l
co-ponents s6ch 4s C!# o8@ects, or ,or prototyping
4in F 1 3Boo does type inference to deter-ine th4t 4in is 4 94l6e type int (integer)
4in F int:#4+&4l6e 3is int ( FF 2,1GH,GI3,JGH)
4lo F 1E000E000L 3is 4 94l6e type lon>
4lo F 1eK20 3is long (FF 100)
4in2 as int 3 (32 8its) or sbyte (I 8its), or s!ort (1J 8its), or lon (JG 8its) integers: 24s2 decl4res 4 94ri48le%s type, de,46lts to 0
in6n 4s uint 3 or byte, us!ort, or ulon 6nsigned integers
4in3 4s int" 3de,46lts to 4nd c4n 8e 4ssigned n6ll
print(4in F int:*4rse(222)) 3!5*s 2
4in F int:*4rse(2-o6se2) 3c46ses 4 Syste-:/or-4t$+ception
9l F 3f 3Boo in,ers the type is 4 sinle floatin point 94l6e type
92 F <0#JL 3is single
93F3:L 3Boo in,ers the type is double floatin$point
,lo as sinle 3,lo4ting point n6-8ers (G 8ytes, 84se 2, 4nd H signi,ic4nt ,ig6res), or double (I 8ytes, 84se 2 4nd 1L signi,ic4nt
,ig6res), or decimal (12 8ytes, 84se 10 4nd penny 4cc6r4te to 2I or 2M signi,ic4nt ,ig6res )
chr F c!ar%&n&' 3the 94l6e type representing 4 single ch4r4cter, 4 System(C!ar (not 4 single ch4r4cter string)
str F "abc" 3 or &def& 3strins 4re i--6t48le re,erence types
print str(Lent! #or len(str) ret6rn the length: !5*s 3
3)scapes in 4 string7 ** 84cBsl4sh, *n ne=line, *r c4rri4ge ret6rn, *t t48, *u%00L0% (G he+ digits speci,ying 4 "nicode ch4r4cter)
3Single A6ote esc4pe in single A6ote strings is *&( o68le A6ote esc4pe in do68le A6ote strings is *"
3String co-p4risons 4re c4se sensiti9e: See the :N$T re,erence on B4sic String !per4tions ,or -ore in,or-4tion
-lstr F 222multi$line strin222 3preser9es sp4cing 4nd line 8re4Bs
8ol F true 3 or false 8oole4n 94l6e types
8ol2 4s bool 3e,46lts to /4lse
ie 4s type* 3indic4tes the 94ri48le (or 4 ,6nction%s ret6rn 94l6e) is 4n I$n6-er48le o, the type speci,ied (eg int*)
,6nctnisp4tch 4s callable 36sed to store n4-e o, 4 ,6nction, -ethod or cl4ss i-ple-enting IC4ll48le to c4ll(see clos6re 8elo=)
c4ll48le h4ndler(4 4s int) 4s int 3'lso c4n de,ine 4 ,6nction%s prototype
c8 4s h4ndler F do(option4l'rg)7 print option4l'rg 3 so 4 clos6re c4n 8e 4ssigned to 4 94ri48le o, th4t type
c8(10) 3!5*s 10
3' list is 4 re,erence type, -6t48le, 94ri48le siCed, o, 4 type deter-ined 8y the lo=est co--on type o, its ele-ents
li F [G, %horse%, L:J,. 3' list o, type o8@ect
li[0. FF G> li[len(li) < 1. FF L:J> li[<1. FF L:J> li[<2. FF %horse%
li2F[. 5; is the e-pty list ;5> li3 F 1, 2, 3 3is 4lso 4 list
do68les F [i;2 ,or i in r4nge(L). 3to ,ill 4 list (see %,or% 4nd %r4nge% de,ined 6nder )ener4tors 4nd 0oops)
li:'dd(%ne= end%)> li:*6sh(%does s4-e 4s 'dd%)> li:'dd(%4%):'dd(%8%)
li:Insert(2,%4,ter horse%)> li:Re-o9e(%horse%)> li:Re-o9e't(1)
li:Sort() 3=o6ld =orB i, 4ll the list ele-ents =ere o, the s4-e type
old04st F li:*op() 3re-o9es D ret6rns l4st ite-
li KF %4 ne= ending% 3(see KF 6nder !per4tions)> li:Inde+!,(%4%)> i, li:Cont4ins(%,oo%)7 print %,ooey%
i, %,oo% in li7 print %,ooey%>
i, %,oo% not in li7 print %no ,ooey%
liG F [[1, 2.;2, [L, J. K [H, I.. 3' list c4n 8e nested =ithin 4nother list
#+rray is 4 re,erence type, -6t48le, o, ,i+ed length, gener4lly =ith the s4-e ele-ent types (4nd gener4lly ,4ster to -4nip6l4te)
4r F (L, 1, 2) 3 or (L, 1, 2,) de,ines 4n 4rr4y o, length 3, initi4liCed 4s indic4ted> 4r[0. FF L> 4r[<1. FF 2
4r2 F 4rr4y%int, 10) 3integer 4rr4y o, ten 0s, or 4rr4y(8yte, (1,2)), or 4rr4y(8yte, (0+//,0+3/)), or 4rr4y(r4nge(3))
4r28 F 4rr4y(i;2 ,or i in r4nge(3)) 3 , or 4rr4y(long, r4nge(3)) or 4rr4y(short, [i ;; 2 ,or i in r4nge(10).), or 4s (o8@ect)
4r3 F 'rr4y:Cre4teInst4nce(int, 2, 2) 3to cre4te 4 2 + 2 integer 4rr4y (c4n cre4te 94rio6s -6ltidi-ension4l 4rr4ys)
4r3:Set&4l6e(MM, 0, 0)> print 4r3:)et&4l6e(0,0) 3!5*s MM
4rG F (of 8yte7 0, 2LL) 3, or (o, short7 <100, 3L, 32HJH), or (o, 'nyType!,!8@ect7 o1, o2)
4rL F 4rr4y(8yte, (0+//, 0+BB, 0+3/))
4rL8F 4rr4y[o, int.(3) 3,or 4n 4rr4y o, 3 ints
4rJ F (,) 3cre4tes 4n e-pty 4rr4y o, type o8@ect < o, little 6se 4s c4n%t store 4nything in it D c4n%t gro= itN
4rH F (2lonely2,) 3cre4tes 4 string 4rr4y =ith 4 single ele-ent
4rI F (1, 2) K (3, G) 34ppends t=o 4rr4ys
#-atri# is 4 re,erence type, -6t48le, o, ,i+ed length, gener4lly =ith the s4-e ele-ent types (4nd gener4lly ,4ster to -4nip6l4te)
-4t F -4tri+(int, 2, 3, G) 3cre4tes 4 2 + 3 + G integer -4tri+ initi4liCed to 0s (4lso o, string, or so-e o8@ect)
-4t2 F -4tri+ o, int(2, 2) 3cre4tes 4 2 + 2 integer -4tri+ initi4liCed to 0s
-4t2[0,0. 3to re,erence
-4t2[071, 071. 3re,erences 4ll o, the ele-ents =ithin the r4nges indic4ted
-4t3 F -4tri+[o, int.(3,3) 3cre4tes 4 3+3 integer -4tri+
len(-4t3, 0) 3ret6rns the length o, the ,irst di-ension
-4t3:R4nB 3ret6rns the n6-8er o, di-ensions
3.as!tables 4re d4t4 str6ct6res th4t 4llo= the stor4ge o, n4-ed ite-s
h4 F O%dog%7%/ido%, %c4t%7%/l6,,y%,P 3cre4tes 4 h4sh t48le
h4[%dog%.F%Spot% 3to ch4nge 4 94l6e
h42 F OP 3cre4tes 4n e-pty h4sh
h4[%8ird%. F %Chirpy% 3to 4dd 4 ne= ite-
h4:+dd(%-o6se%, %SA6eeBy%) 3to 4dd 4 ne= ite-
h4[%c4t%. FF %/l6,,y% 3ret6rns Tr6e
h4:Contains/ey(%-o6se%)> h4:ContainsValue(%/l6,,y%) 38oth ret6rn Tr6e
h4:Remove(%dog%)
h4[284dBey2. 3ret6rns n6ll
h43 F H4sh((i, i;2) ,or i in r4nge(L)) 3See :N$T%s 2H4sht48le Cl4ss2 ,or 4ddition4l -ethods D properties Boo%s h4sht48le inherits
,or ite- in h437 print ite-:Qey, 272, ite-:&4l6e 3to print e9erything in 4 h4sh t48le
3)numer4tions cre4te 4 type =ith 4 set o, i--6t48le 2n4-e to 32 8it integer 94l6e 4ssoci4tions2, st4rting, 8y de,46lt, 4t 0
en6- Tho6ghts7
Boring> 1ise> H46ghty 3 c4n 8e spre4d 4cross sep4r4te lines, or sep4r4te loc4tions or ,iles (6se the partial -odi,ier in 8oth
3 pl4ces): BoringFM or H46ghtyF<3 o9erride de,46lt 94l6es
,or 94l6e in Syste-:$n6-:)et&4l6es(Tho6ghts)7 print 94l6e, 2F2, 94l6e c4st int
ti F L0s 3cre4tes 4 timespan 94ri48le =ith 94l6e 007007L0 other ,or-s7 2-, :Mh, 3d, or G2-s
t2 F 10:0e<Gh 33:J seconds
t F int 3or string, or :::, 4ssigns 4 type to 4 94ri48le to cre4te 4 R6nti-eType
void 3the de,46lt 94l6e o, 4n 6n4ssigned re,erence type> not 94lid ,or 4 94l6e type (e+cept 94l6e types ending =ith N (eg intN))
unsafe ptr 4s long F 4str6ct7 ;ptr F 0> KKptr 34llo=s pointer -4nip6l4tion ,or richer interoper48ility =ith n4ti9e code
0perations
4 1 8 3I, 4 4nd 8 4re 94l6e types (incl6ding str6cts), 8%s 94l6e(s) 4re copied into 4
3 I, 4 4nd 8 4re re,erence types, 4 is -4de to re,erence the s4-e o8@ect 4s 8
#Careful not to use 1 to compare types %use 11 instead'
3#4ny o, the ,ollo=ing c4n 8e o9erlo4ded to ch4nge the de,46lt 8eh49io6r =hen de,ining types
3#at! operators (,ro- highest to lo=est precedence)7 4 ; 8, 4 / 8, 4 ;; 8 (e+ponent), 4 2 8 (re-4inder), 4 3 8, 4 < 8
3)ro6ping in p4rentheses incre4ses the priority o, the oper4tions cont4ined
3 Boo =ill do type coercion (eg i, 4ny oper4nd is 4 do68le, the others 4re con9erted to 4 do68le)
3Loical operators7 4 RR L (8it shi,t right), 4 SS J (8it shi,t le,t, T4 (ones co-ple-ent), 4 4 8 (8it=ise 4nd), 4 5 8 (8it=ise or),
4 6 8 (8it=ise e+cl6si9e or), 4 7 8 (less th4n), 4 8 8 (gre4ter th4n), 4 71 8 (less th4n or eA64l to), 4 81 8 (gre4ter th4n or eA64l to),
4 11 8 (eA64lity < tests ,or eA64l 94l6e ,or 94l6e types (incl6ding str6cts), or pointing to the s4-e o8@ect ,or re,erence types),
4 91 8 (ineA64lity (o, 6neA64l 94l6e ,or 94l6e types, not pointing to the s4-e type ,or re,erence types)),
4 and 8, 4 or 8, not 4 34 4nd 8 sho6ld 8e 8oole4ns ,or these
isa #Ret6rns TR"$ i, re,erenced 94ri48le is o, cl4ss or 84se cl4ss, or i-ple-ents, the inter,4ce speci,ied (6se,6l prior to c4sting)
print %str% isa string 3!5*s Tr6e
4ssert 4r2 is4 (int) 3)ener4tes 4n e+ception i, 4r2 is not 4n integer 4rr4y
is #Ret6rns TR"$ i, t=o re,erence types point to the s4-e o8@ect, or to test ,or n6ll
(11 4lso does this, pl6s =orBs ,or 94l6e types 4s =ell)
is not #Ret6rns TR"$ i, t=o re,erence types don%t point to the s4-e o8@ect
print(o1 is o2) 3!5*s /4lse (o1 4nd o2 4re di,,erent e+4-ples o, the s4-e type)
print (o2 is null) 3!5*s /4lse
h, i, @ F 1, 2, 3 3multiple assinment (h FF 1, i FF 2, @ FF 3)
B F 2sp4-2, G2, o8@ect() 3B 8eco-es 4 list o, o8@ects (the lo=est co--on deno-in4tor)
4, 8, c F O i ;F 2> ret6rn i P() ,or i in r4nge(1, 10) 34 FF 2, 8 FF G, c FF J>
h, i F i, h 3to s:ap 94l6es
d 4s int F 3, 2, 1 3d FF 3, rest o, 94l6es 4re disc4rded
l4stN4-e F %Si-pson%> e, , F l4stN4-e> print e, , 3!5*s S i
4in F 123:G cast int 3tr4ns,or-s one d4t4 type to 4nother> co-piler c4tches in94lid c4sts so 8etter ,or 94l6e types
4in2 F c4st(8yte, 2LL) 3co-piler c4tches in94lid c4sts
4in3 F %123% as (int) 3silently ret6rns n6ll i, tr4ns,or-4tion o, 4 re,erence type is in94lid (4s in this e+4-ple)
s F 2 K 3:L 3s is 4 do68le (the -ost cont4ining ,or-)
33in 34dds one to 4 n6-eric 94ri48le 8e,ore 6sing it in 4n e+pression, 4lso $$in
in33 4dds one to 4 n6-eric 94ri48le 4,ter 6sing it in 4n e+pression, 4lso in$$
in 31 L 3eA6i94lent to in F in K L, 4lso =orBs ,or the other -4th 4nd 8it shi,t oper4tors
t F %48c%> t KF %d% 3=orBs ,or strings 4s =ell
print 2n6-8er ;s in the -iddle is strin interpolation2 3!5*s n6-8er L:L in the -iddle is string interpol4tion
print 2n6-8er ;%s 3 <' in the -iddle2 3!5*s n6-8er H:L in the -iddle
3string interpol4tion in do68le or triple A6oted strings only, ?U to esc4pe
3strin formattin, see :N$T%s St4nd4rd N6-eric /or-4t Strings ,or -ore e+4-ples
n6- F 0123GLJHIM> print n6-:ToString(2(033) 333<33332) 3!5*s (012) 3GL<JHIM
s F 2Right no= it is O0P: S4y hi to O1P:2> print s V (d4te:No=, 2#4ry2)
print 2O0,<107dGP ispl4ys le,t<4ligned in 10 col6-ns 01232 V (123,)
print 2O07,2P displ4ys 2:G0, O17g2P displ4ys J2 V (2:G, J,)
print 2O07e2P displ4ys 2:G0eK0002 V (2:G,)> print 2O07cP displ4ys U2,M23:G02 V (2M23:G,)
%,oo% K %84r% FF %,oo84r%
%,oo%K3 FF %,oo3%
%,oo%;3 FF %,oo,oo,oo%
3Slicin e+tr4cts 4 r4nge o, ele-ents ,ro- string, list, 4nd 4rr4y cont4iners
res6lt F slicee[,irstInde+ 7 inde+Not14nted 7 step.
3 ,irstInde+ de,46lts to 0, inde+Not14nted de,46lts to the cont4iner length, step de,46lts to 1
3 neg4ti9e indices co6nt 84cB ,ro- the l4st entry (=hich is 4t inde+ <1)> i, inde+ is too neg4ti9e, 0 is 6sed
3 slicing not 4llo=ed on the le,t side o, 4n e+pression
cont4iner2 F cont4iner[7. to clone 4ny slice48le type
1 in (2, 3, 1)> 2,oo2 not in (284C2, 28oo-2) 3Tests the presence o, 4n ite- in 4 set
3( 4llo=s 4ccessing the ele-ents o, 4 hier4rchy 4s in -yCl4ss:its#ethod()
3)#ception !andlin
try7 i F 0> @ F 10 5 i 3do so-ething th4t -ight ,4il
e#cept e 4s i9ideByWero$+ception7 3c4tching 4 speci,ic e+ception type
print e
raise 3recre4te origin4l e+ception (r4ise c4n 4ppe4r o6tside 4n e+cept =ith 4rg6-ents)
e+cept so-eCondition4lSt4te-ent7 3i, 4n e+ception occ6rs, D the conditions 4re -et, e+ec6te the ,ollo=ing st4te-ent5code 8locB
e+cept e7 print(2ooops::: 1e%9e got 4n error7 2 K e) 3C4tch<4ll to h4ndle error types not c46ght 8y pre9io6s e+cepts
r4ise 2$+ception4l -ess4ge or 4 Syste-:$+ception2 3See the :N$T%s 2Syste-$+ception Cl4ss2 ,or e+ception4l det4ils
c!ecked7 3t6rns on Syste-:!9er,lo=$+ception checBing ,or 4 st4te-ent or code 8loc (the de,46lt)
try7
B F 10SSJ3> - 4s int F B 3C46ses !9er,lo= $+ception 4s the long%s 94l6e =on%t ,it in the 32 8it int
e#cept +7 print +:)etType():N4-e 3prints e+ception n4-e
ensure= print 2no= cle4n 6p2 3code 8locB e+ec6ted =hether or not 4n e+ception occ6rred
unc!ecked7 3t6rns o,, Syste-:!9er,lo=$+ception checBing ,or 4 st4te-ent or code 8loc
@ F int:#4+&4l6e> @KK
assert so-e0ogic4l$+pression 3i, so-e0ogic4l$+pression is ,4lse, 4n 'ssertion/4iled$+ception is thro=n
#reular e#pressions pro9ide p4ttern 84sed se4rch 4nd repl4ce oper4tions on te+t
i, s4-plestring 1> /,oo/7 3*erl%s -4tch oper4tor
,n4-e, ln4-e F 5(?=K)5:#4tches(2 $ric Idle 2)> print ,n4-e, ln4-e 3!5*s 2$ric2 4nd 2Idle2
,n4-e, ln4-e F X5 5:Split(2$ric Idle2)>print ,n4-e>print ln4-e 3!5*s 2$ric2 4nd 2Idle2 86t s6pports =hite sp4ce D t48s
oto l48el 32=label2 needed < =4y o6t o, ,4shion
goto l48el i, (@FF10)
Conditionals
if i FF 17 3Boole4n e+pression =hich, i, tr6e, c46ses 8locB to 8e e+ec6ted> C'R$/"0 N!T T! S'( i F 1 IN TH$ H$'$R
3indented 8locB o, st4te-ents (4 code 8locB) ended 8y the dedent
36se t48s ,or indents (NB d4ngero6s to -i+ t48s D sp4ces ,or le4ding indents)
pass 3the -ini-6- content o, 4n indented code 8locB
elif i FF 27 3option4l < 4nother test
p4ss 38locB to e+ec6te
else7 3option4l < p4th to t4Be =hen i,, D eli,s ,4il> else -6st ,ollo= 4ny eli,s
p4ss 34n 4ltern4te 8locB
i, i FF 17 @ F 2 3sets @ to 2 i, i eA64ls 1
i, not r7 @ F 2 3sets @ to 2 i, r is ,4lse
print 2Tr6e2 i, i FF 1 3' statement :it! modifier < e4sier to re4d ,or- i, only doing one thing
lt64e F (G2 i, tr6e else n6ll)
i, tr6e7 i F 1> @ F 2
unless i FF 17 @ F 2 3sets @ to 2 i, i not eA64l to 1 (e+ec6tes the st4te-ent or code 8locB i, the e+pression is ,4lse)
c, d F 3, G 6nless ,4lse 3sets c to 3, 4nd d to G
+ ?unction is 4 seA6ence o, st4te-ents th4t per,or-s 4 speci,ic t4sB, option4lly c4lled =ith 4rg6-ents, 4nd ret6rning 4 94l6e
def 4/6nction (p1 4s int, p2, [re@uired%p3 R G). p3 4s int, ref pG, [default(%)ood to goY%). pL, *pJ 4s (string)) 4s deci-4l7
3' ,6nction he4der is ,ollo=ed 8y 4n indented 8locB o, st4te-ents, ended 8y the dedent (pass is the -ini-6- 8locB)
3/6nctions c4n 8e de,ined =ithin 4nother ,6nction (see clos6re)
3' ,6nction c4n c4ll itsel, (c4re,6l to pro9ide 4n e+it ,ro- the rec6rsion
3Aarameters7 p2 de,46lts to type o8@ect (=hich is =hy its 4 good ide4 to speci,y 4 p4r4-eter%s type),
p3 c4n%t 8e n6ll 4nd -6st e+ceed G (else 4 Syste-:'rg6-ent$+ception =ill 8e r4ised)
pG is 4 94l6e p4r4-eter p4ssed 8y re,erence (so ,6nction c4n -odi,y c4ller%s copy),
pL h4s 4 de,46lt pro9ided in c4se its c4lled =ith n6ll,
;pJ (-6st 8e l4st) speci,ies 4n 4rr4y (o, type string (de,46lts to o8@ect)) =hich 4llo=s ,or 4 94ri48le n6-8er o, p4r4-eters
324s deci-4l2 speci,ies the type the ,6nction ret6rns (de,46lts to 9oid)> Boo =ill 4pply type in,erence to deter-ine this i, it c4n
return option4l&4l6e 3ret6rns control to the ,6nction%s c4ller =ith option4l 94l6e
3ropping o,, the 8otto- o, the code (or ret6rn =itho6t 4n 4rg6-ent) ret6rns the de,46lt 94l6e ,or the ,6nction%s ret6rn type
3' ,6nction c4n 2ret6rn +, y2 i, c4lled 4s in 24, 8 F 4/6nct()2, or ret6rn 4 gener4tor 4s in 2ret6rn [i;2 ,or i in r4nge(L).2,
or ret6rn -6ltiple 94l6es 8y 6sing re,erence p4r4-eters (6se sp4ringly), or 8y ret6rning 4 co-ple+ type (s6ch 4s 4n 4rr4y)
yield 94l6eTo(ield 34llo=s 4n en6-er48le ,6nction to ret6rn to yield 6p one 94l6e, D 8e ret6rned to so 4s to yield so-e -ore
8/6nction J 3!Q to drop the p4renthesis =hen c4lling 4 ,6nction, pro9ided yo6%re not 6sing 4 ret6rn 94l6e
ping(1)(2) 3'llo=s the ,6nction ret6rned 8y ping to 8e c4lled =ith the 4rg6-ent 2
3' ,6nction is re4lly 4 st4tic -ethod o, the 46to-4tic4lly gener4ted -od6le cl4ss: 's s6ch, ,6nctions c4n 8e o9erlo4ded:
3' ,6nction c4ll, or 4 n4-eless code 8locB (see clos6res), c4n 8e s49ed 4ny=here 4 94l6e c4n
3BeinBnvoke (S,6nction 4rgs, R c4ll84cB 4s 'syncC4ll84cB, 4syncSt4te) 4s I4syncRes6lt in9oBes the ,6nction async!ronously
4nd ret6rns 4 h4ndle th4t c4n 8e 6sed to checB the st4t6s o, the e+ec6tion 4s =ell 4s reco9ering the ret6rn 94l6e
#)ndBnvoke (4syncRes6lt 4s I'syncRes6lt) 4s S,6nction ret6rn typeR7 3,inishes 4n 4synch in9oc4tion ret6rning the ,in4l 94l6e
de, r6n()7
print(2e+ec6ting2)
res6lt F r6n:BeginIn9oBe(O print(2c4lled 84cB2) P) 3'ltern4tely7 res6lt F r6n:BeginIn9oBe(c4ll84cB, n6ll)
Syste-:Thre4ding:Thre4d:Sleep(L0-s) 3 =here de, c4ll84cB(res6lt 4s I'syncRes6lt)7
r6n:$ndIn9oBe(res6lt)
Bnterfaces pro9ide 4 speci,ic4tion o, ,ields, properties D -ethods th4t cl4sses th4t i-ple-ent it -6st ,ill in
inter,4ce#odi,iers interface I#o8ile (option4lInter,4ce)7 3n4-ed 8y con9ention 4s 4n I, ,ollo=ed 8y *4sc4lC4se
3inter,4ce#odi,iers7 partial (c4n 8e spre4d 4cross sep4r4te loc4tions or ,iles < 6se p4rti4l in 8oth pl4ces)
Speed*roperty 4s int7 3inter,4ce de,initions (properties D -ethods) 4re only te-pl4tes
get 3 D reA6ire the i-ple-enting cl4ss to ,ill the- o6t
de, ch4ngeSt4te#ethod(p4r4-etersI,'ny) 4s 8ool 39oid is the de,46lt ret6rn type, -ethods c4n%t h49e 4 8ody (p4ss is 4llo=ed)
+ Class is 4 te-pl4te th4t inst4nces its Bind o, o8@ect, D ret6rns 4 re,erence type to the inst4nti4tion =hen c4lled 6pon:
3 It c4n inherit ,ro- one re,erence type it is 4n e+4-ple o,, 4nd it c4n i-ple-ent -6ltiple inter,4ces (they -6st 8e listed l4st):
3 ' cl4ss -4y cont4in ,ields, properties, constr6ctor -ethods, -ethods, e-8edded cl4sses, 4nd 4 destr6ctor -ethod
cl4ss#odi,iers cl4ss *erson (#4--4lCl4ssIInherit, Inter,4cesI,'ny)7 3p4renthesis not needed i, no inherit4nces nor inter,4ces
3cl4ss#odi,iers7 partial (c4n 8e spre4d 4cross sep4r4te loc4tions or ,iles < 6se p4rti4l in 8oth p4rts),
protected (only 4ccessi8le =ithin cont4ining cl4ss), private (protected 86t descend4nt cl4sses c4n 4ccess), public (the de,46lt),
internal (only 4ccessi8le to its 4sse-8ly),
abstract (4 cl4ss th4t inherits ,ro- this -6st i-ple-ent 4ll 48str4ct properties D -ethods), final (c4n%t 8e inherited ,ro-),
static (cl4ss doesn%t need to 8e inst4nced ,or it to e+ist), transient (cl4ss not to 8e seri4liCed)
,ield#odi,iers E,ieldN4-eF%green% 3loc4l 94ri48le (2E2 option4l)>fields c4n 8e 94l6e or re,erence types (4 cl4ss c4n h49e cl4sses)
3/ields (4nd properties) 4re initi4liCed ,or e4ch inst4nti4tion o, 4 cl4ss, 4nd 4re 4 cl4ss% loc4l 94ri48les
3,ield#odi,iers7 protected (the de,46lt7 only 4ccessi8le =ithin cl4ss), private (protected 86t descend4nt cl4sses c4n 4ccess),
public (the =orld c4n ch4nge), transient (,ield not to 8e seri4liCed), static (cl4ss doesn%t need to 8e inst4nced ,or ,ield to e+ist),
final (c4n%t 8e ch4nged 4,ter set (=hen de,ined, or 8y the constr6ctor)), static final is 4 co-pile ti-e const4nt,
virtual (cl4ss th4t inherits ,ro- this -4y o9erride this ,ield), override (6se this ,ield inste4d o, 4 9irt64l ,ield in p4rent cl4ss)
property#odi,iers *ropN4-e 4s string7 3Aroperty is 4 port4l 2 4 loc4l ,ield =hose 4ccess is de,ined 8y the presence o, get D set
3property#odi,iers7 public (the de,46lt), abstract (4 cl4ss th4t inherits ,ro- this -6st i-ple-ent this property),
4nd -ost o, the ,ield -odi,iers de,ined 48o9e
*ropN4-e 4s string7
et7 ret6rn E,ieldN4-e 3property c4nZt 8e re4d o6tside cl4ss =itho6t get> ret6rn c4n h49e 4n e+pression, not @6st 94ri48le n4-e
set7 E,ieldN4-e F 94l6e:To"pper() 3property c4nZt 8e set ,ro- o6tside cl4ss =itho6t set> 94l6e c4n in9ol9e 4n e+pression
3' sett48le property (or p68lic ,ield) c4n 8e set =hen 4 cl4ss is inst4nced (eg: p F *erson(/irstN4-e7 %*eter%)
[property(#iddleN4-e). E-iddleN4-e F %T% 3cl4ss property #iddleN4-e is re4d5=rite, 4nd de,46lts to %T%
[property(NicBN4-e, 94l6e is not n6ll 4nd len(94l6e) R 0). EnicBN4-e F 2A6eN2
[property(/irstN4-e, +ttributes=CRe@uiredD'D E,irstN4-e 4s string 3cl4ss property /irstN4-e c4n%t 8e set to n6ll
[property(NoN4-e, *rotected7 tr6e). EnoN4-e 4s string 3cl4ss property NoN4-e only 9isi8le =ithin cl4ss D inheritors
e-o[s 4s string.7 3'n initi4l<48le, =4y to de,ine 4 re4d<only property th4t yo6 h49e to pro9ide the 94l6e to re4d
get7ret6rn s:To"pper() 3gi9en the cl4ss in9oc4tion p, 2print p:e-o[%s%.2 to get
Ite-[i, @.7 3' property de,ined th6sly, c4n 8e re,erenced in 4 get =ith 4rg6-ents> gi9en the cl4ss in9oc4tion t48le,
get7 ret6rn 2Ui, U@2 3 4s in 2print t48le:Ite-[1, 2.2
constr6ctor#odi,iers de, constructor(option4l*4r4-eters)7 3Is c4lled (=ith option4l p4r4-eters) to per,or- initi4liC4tion
=hen cl4ss is inst4nced: I, the p4rent cl4ss% constr6ctor t4Bes no 4rg6-ent, it is c4lled 8e,ore this constr6ctor
3constr6ctor#odi,iers= static -e4ns the constr6ctor -ethod is c4lled (=itho6t p4r4-eters) to initi4liCe st4tic ,ields ,or the cl4ss
the ,irst ti-e the cl4ss is inst4nced
3option4l*4r4-eters c4n h49e the s4-e p4r4-eter options 4s ,or ,6nction p4r4-eters pre9io6s de,ined
3C4n h49e -6ltiple constr6ctors =ith di,,erent p4r4-eters D they c4n c4ll e4ch other
-ethod#odi,iers def -ethodN4-e(option4l*4r4-eters) 4s int7 3' met!od is 4 ,6nction pro9ided 8y the cl4ss
3-ethod#odi,iers7 static ((cl4ss doesn%t need to 8e inst4nced ,or it to 8e c4lled),
final (the de,46lt7 c4n%t 8e o9erridden),
abstract (4 cl4ss th4t inherits ,ro- this -6st i-ple-ent this -ethod),
public (the de,46lt7 9isi8le o6tside its cl4ss),
virtual (4 cl4ss th4t inherits ,ro- this -4y o9erride this -ethod),
override (this -ethod to 8e 6sed inste4d o, p4rent cl4ss% 48str4ct or 9irt64l -ethod (o9erridden -ethod pro9ides ret6rn type),
callable (4llo=s 4 -ethod%s n4-e to 8e stored in 4 94ri48le o, type c4ll48le),
de, -ethodN4-e(option4l*4r4-eters) 4s string7 3c4n rede,ine -ethods =ith di,,erent p4r4-eters, or 4 di,,erent ret6rn 94l6e
3option4l*4r4-eters c4n h49e the s4-e options 4s ,or ,6nction p4r4-eters pre9io6s de,ined
3The de,46lt ret6rn type o, 4 -ethod =ill the -ost generic type 4-ong the types o, the e+pressions 6sed in ret6rn st4te-ents:
de, destructor()7 3the syste- c4lls this option4l -ethod =hen 4n o8@ect is 48o6t to 8e ,reed: Ne9er c4ll e+plicitly
de, EoStrin()7 3'llo=s 4n o8@ect to c6sto-iCe =h4t h4ppens =hen it is printed (de,46lts to the cl4ss n4-e)
de, )@uals( + 4s *erson )7 3'llo=s 4n o8@ect to de,ine eA64lity (4n 4ltern4ti9e to the oper4tor o9erlo4d opE$A64lity)
de, Fet.as!Code()7 3'llo=s 4n o8@ect to ret6rn so-ething th4t 6niA6ely Is it (de,46lts to o8@ect%s 4ddress)
[$+tension. 3'llo=s -ethods D properties to 8e 4dded to 86ilt<in cl4sses
def ToInt(s 4s string)7 3This e+4-ple e+tends the string type
ret6rn int:*4rse(s) 3 to p4rse 4n int o6t o, it
4 F %123%> i F 4:ToInt 3To 6se
30perator overloadin is 4cco-plished 8y de,ining -ethods n4-ed opE'ddition, opES68tr4ction, opE#6ltiply,
opEi9ision ,opE#od6l6s, opE$+ponenti4tion, opE$A64lity, opE0essTh4n, opE0essTh4n!r$A64l, opE)re4terTh4n,
opE)re4terTh4n!r$A64l, opE#4tch (2TF2), opENot#4tch (2YF2), opE#e-8er (2in2), opENot#e-8er (2not in2), opEBit=ise!r,
opEBit=ise'nd, opE"n4ryNeg4tion,
opEI-plicit (ret6rns the proper type (this -ethod%s type), =hen one o, 4 cl4ss% -ethods is c4lled =ith the =rong type
(=hich -4tches opEI-plicit%s p4r4-eter)
31hen 4 8in4ry 4rith-etic oper4tor s6ch 4s opE'ddition is o9erlo4ded, the corresponding 4ssign-ent oper4tor KF is 4s =ell
3The FF oper4tor c4n 8e o9erlo4ded ,or 4 cl4ss 8y pro9iding 4n $A64ls -ethod th4t is c4lled =ith 4nother e+4-ple o, the cl4ss
4s 4 p4r4-eter, 4nd ret6rns tr6e i, the t=o 4re eA64l (8y =h4te9er -e4s6re-ent 4pplies)
3!per4tor o9erlo4ding 4pplies to str6ct6res (see str6ct 8elo=) 4s =ell
cl4ss *oint7
+ 4s int> y 4s int
de, constr6ctor (ne=[ 4s int, ne=( 4s int)7
+ F ne=[> y F ne=(
de, opE'ddition (p1 4s *oint, p2 4s *oint)7
p1:+ KF p2:+> p1:y KF p2:y> ret6rn p1
o9erride de, ToString()7
ret6rn 2+ F U+, y F Uy2
4 F *oint(1,2)> print (4 KF *oint(3,G))
super: 36sed to re,er to ,ield or -ethod o, 4 cl4ss descended ,ro-: 1itho6t s6,,i+ (@6st 2s6per2) it c4lls the s4-e n4-ed -ethod
s6per() 3, or s6per:)etType()
self( 36sed to re,erence the inst4nce%s 9ersion o, 4 ,ield in c4se o, 4 n4-e con,lict, other=ise not necess4ry
3sel,(option4l'rg6-ents) ,ro- 4 constr6ctor, c4lls 4nother constr6ctor o, the cl4ss =hose p4r4-eters -4tch option4l'rg6-ents
*erson:st4tic#ethod() 3c4lling 4 cl4ss% st4tic -ethod (cl4ss doesn%t h49e to 8e inst4nced), =ith option4l property setting 4rgs
*erson():4ny#ethod() 3inst4ncing(=itho6t 4ssigning 4 cl4ss re,erence to 4 94ri48le) D c4lling 4 -ethod(constr6ctor is c4lled 1st)
p F *erson(/irstN4-e7%*eter%) 3inst4ncing 4 cl4ss D setting its p68lic ,ields5properties)
r9 F p:-ethodN4-e() 3c4lling 4n inst4nced cl4ss%s -ethod, =ith option4l ret6rn 94l6e, 4nd option4l 4rg6-ents
cl4ss /oo7
[property(/irst). E,irst F n6ll
[property(Second). Esecond F n6ll
de, constr6ctor(94l6e)7
/irst, Second F 94l6e
, F /oo([2,irst2, 2second2.) 3/irst FF 2,irst2, Second FF 2second2
cl4ss !6ter7 3Cl4sses c4n 8e cont4ined =ithin cl4sses (8y de,46lt they 4re 9isi8le ,ro- o6tside) 4nd c4n 8e cre4ted =itho6t
cl4ss Inner7 3 ,irst cre4ting the o6ter cl4ss (eg !6ter:Inner())
st4tic de, /oo()7
print(2UInner2)
!6ter:Inner:/oo() 3!5*s !6terKInner:/oo (/oo is st4tic so doesn%t need 4ny cl4ss cre4tion)
!6ter:Inner():/oo() 3cre4tes 4n Inner, then c4lls its /oo -ethod
8 4s ISo-eInter,4ce F So-eCl4ss() 38 is restricted to 4ccessing the properties 4nd -ethods o, ISo-eInter,4ce
c F c4st(So-eCl4ss, 8) 3e+tends 4ccess to 4ll the properties 4nd -ethods o, So-eCl4ss
d 4s *4rentCl4ss F 46ghterCl4ss() 3d is restricted to 4ccessing the properties 4nd -ethods o, *4rentCl4ss,
6nless 4 -ethod =4s decl4red 9irt64l in *4rentCl4ss, 4nd is decl4red o9erride in 46ghterCl4ss
e F c4st(46ghterCl4ss, c) 34llo=s 4ccess to the properties 4nd -ethods o, 8oth cl4sses
ICo-p4r48le 3I, 4 cl4ss 6ses this inter,4ce, it -6st i-ple-ent 4 2de, Co-p4reTo (other) 4s int72 -ethod =hich ret6rns 4 neg4ti9e
i, this o8@ect is less th4n the other, 0 i, eA64l, positi9e i, this is gre4ter: This 4llo=s 4 collection o, this type to 8e sorted
Fenerators and Loops
rane (end) 3is 4n enumerable (4 ,6nction th4t ret6rns 4 seA6ence o, 94l6es) th4t e,,iciently gener4tes integers ,ro- 0 to end < 1
r4nge (st4rt, end) 3gener4tes integers ,ro- st4rt to end \ 1: end -4y 8e S st4rt
r4nge (st4rt, end, step) 3incre-ent (-4y 8e decre-ent)
for ii in r4nge(J)7 3 or 2,or ii in [10, G, 12.72, or 2,or ii in (1, 2, 3)72, or 2,or 4, 8 in [(1, 0), (3, 0), (2, 0).72
3indented code 8locB (possi8ly 6sing 94ri48le ii) th4t =ill 8e repe4tedly e+ec6ted
print(@oin(i;2 ,or i in r4nge(10) i, 0 FF i V 2)) 3!5*s 0 G I 12 1J
ite-s F (+, y) ,or + in r4nge(1, 3) ,or y in r4nge(3, 0)
:!ile r7 3r is 4 8oole4n e+pression
3indented code 8locB to e+ec6te =hile r is tr6e
continue 3sBips to the ne+t iter4tion o, 4 ,or or =hile loop
contin6e i, i R L 34nd 2contin6e 6nless @ S202 4lso possi8le
break 3e+its 4 ,or or =hile loop
8re4B 6nless i S 10 4nd i R L
or7 34 st4te-ent or code 8locB to e+ec6te i, the ,or5=hile 8locB doesn%t e+ec6te (nothing s4tis,ies the conditions)
t!en7 34 st4te-ent or code 8locB to e+ec6te 4,ter the ,or5=hile 8locB (pro9ided the ,or5=hile doesn%t 8re4B)
r F tr6e> i F 0
=hile r7
8re4B 6nless i R L 4nd i S 10
or7 print 2idn%t loop2
then7 print 2idn%t 8re4B o6t2
#Fenerator e#pressions < 6se 4ny =4y 4 94ri48le c4n 8e 6sed to prod6ce 4 seA6ence o, 94l6es incl6ding 8eing stored in 4
94ri48le ,or l4ter (D repe4ted) e+ec6tion
en6-er F + ; 2 ,or + in r4nge(1,10) i, + YF L
list F [ite- ,or ite- in en6-er. 3The res6lting en6-er48le c4n 8e 6sed -4ny =4ys, incl6ding pop6l4ting 4 list
yield so-e&4l6e 3' 6ser de,ined en6-er48le ,6nction =ill yield so-e&4l6e, only to 8e ret6rned to, to gener4te so-e -ore
3)numerable types h49e 4 )et$n6-er4tor -ethod th4t ret6rns 4 $n6-er4tor cl4ss (c4n 8e intern4l) =hich i-ple-ents
the I$n6-er4tor inter,4ce, 4 #o9eNe+t -ethod to point to the ,irst5ne+t C6rrent (ret6rns tr6e i, p4st the end),
4 C6rrent property, 4nd 4 Reset -ethod to point 84cB to the 8eginning (4ll o, =hich 4re h4ndled 8y de,46lt 8eh49io6rs i,
4 ,6nction @6st =4nts to yield)
de, onet=othree()7
yield 1> yield 2> yield 3
e F onet=othree():)et$n6-er4tor()
=hile e:#o9eNe+t()7 print(e:C6rrent) 3!5*s 1 2 3
Fenerics 4llo= cl4sses, -ethods, D inter,4ces to 8e de,ined s6ch th4t they -4y 8e re6sed 4cross di,,erent types
cl4ss St4cB[of T.7
Eposition F 0
Ed4t4 F 4rr4y(T, 100)
de, p6sh(o8@ 4s T)7
Ed4t4[EpositionKK. F o8@
de, pop() 4s T7
ret6rn Ed4t4[<<Eposition.
4FSt4cB[o, int.()
8FSt4cB[o, string.()
de, ,6nction[o, T.(4rg 4s T)7
print T
,6nction[o, int.(3)
Structures 4re 6ser de,ined 94l6e types: The 4ssign-ent oper4tor copies the =hole thing
struct Testy7
Ei 4s int 38y de,46lt ,ields 4re p68lic
E@ 4s int 3' p68lic ,ield c4n 8e set =hen 4 str6ct is inst4nced, eg: 4 F Testy(3, E@7L)
de, constr6ctor(+ 4s int)7 3Cre4te 4nd 4ccess ,ields D -ethods 4s ,or 4 cl4ss
Ei F + 3oper4tor o9erlo4ding possi8le =ith str6cts (see the de,inition o, cl4ss)
o9erride de, EoStrin()7 3'llo=s 4 str6ct6re to c6sto-iCe =h4t h4ppens =hen it is printed (its str6ct6re type is the de,46lt)
ret6rn 2UEi2
c F Testy(L) 3str6cts 4re ,4ster to cre4te 4nd dispose o, th4n cl4sses
Closures are 8locBs o, code th4t c4n 8e p4ssed 4s 4n 4rg6-ent in 4 ,6nction c4ll, or s49ed in 4 94ri48le
c F do(option4l'rg)7 print option4l'rg 38locB 84sed e+4-ple (c4n 8e -6lti<line indented 8locB)
i-port Syste-:1indo=s:/or-s 38r4ces 84sed e+4-ple
86tton1 F B6tton(Te+t7 2#e tooY2)
86tton1:ClicB KF O print 2ClicB -e2>
print 2=hitesp4ce is ignored inside OP so yo6 need to 6se se-icolons to incl6de -6ltiple st4te-ents:::2>
P
clos6re 4s c4ll48le(int) 4s string F O i ] i:ToString(20002) P 3the 20002 4rg6-ent speci,ies ,or-4tting
print clos6re(H) 3!5*s 00H
)vents 4llo= 4n o8@ect%s cre4tor to pro9ide code to h4ndle 4n e9ent =hen it ,ires
cl4ss ClicBer7
e9ent#odi,iers e9ent ClicBed 4s )vent.andler
3e9ent#odi,iers7 transient (e9ent not to 8e seri4liCed)
de, R4iseClicB()7
ClicBed(sel,, n6ll)
p F ClicBer()
p:ClicBed KF def(sender, 4rgs)7 print 2T4h C0ICQ$Y2 3clos6re pro9ides 4nother e9ent h4ndler (c4n 6se 2do2 inste4d o, 2de,2)
p:R4iseClicB()
3"se 2<F2 to l4ter det4ch 4 clos6re ,ro- 4n e9ent
Built$in -acros and ?unctions
print(
222's<is,
4 -6lti<line print to St4nd4rd !6t
222)
print(string:?ormat(2O0P, O1P, O2P2, 212, 2, 3))
print BooVersion
print 3=itho6t 4rg6-ents, !5*s ne=line
print Goin(4rr4) 3@oins 4ll ele-ents in 4n en6-er48le, ret6rning 4 string 6sing 4 sp4ce deli-iter
print Goin(4rr4, ch4r(%K%)) 34s 48o9e 6sing %K% 4s deli-iter
print 94ri48leN4-e:FetEype() 3o6tp6ts the type o, 94ri48leN4-e
print (s F 0ist(%48cde,ghi%)) 3!5*s [4, 8, c, d, e, ,, g, h, i.
,or -e-8er in typeof(o8@ect):)et#e-8ers()7 print -e-8er:N4-e
,or i 4s int, o8@ in enumerate([242, 282, 2c2.)7 print i, 272, o8@ 3prod6ces 4 r6nning co6nt =hile looping thro6gh ite-s
reversed(4n$n6-er48le) 3ret6rns, 4s 4n I$n6-er48le, ite-s in 4n en6-er48le in re9erse order
4 F iterator ([1,2,3,G,L.)> ,or i in 47 print i 3ret6rns 4n en6-er4tor ,or the o8@ect p4ssed < else it 8lo=s: %,or% does it gr4ce,6lly
#reversed ret6rns 4n iter4tor ,or 4 seA6ence in re9erse order
ls F [1,2,3,G.> print([n ,or n in re9ersed(ls).) 3!5*s [G, 3, 2, 1.
map ([%el1%,%el2%., ,6nct()) 3ret6rns 4n I$n6-er48le o8@ect th4t res6lts ,ro- 4pplying ,6nc to e4ch ele-ent o, 4 en6-er48le
-4p([1,2,3,G,L,J.) de, (+ 4s int)7 ret6rn +;+;+ 3 -4p 6sing 4n anonymous closure
Hip((%4%,%8%,%c%), (%'%,%B%,%C%)) 3interle49es t=o en6-er48les, ret6rning 4n I$n6-er48le o8@ect, e4ch ele-ent o, =hich
is 4 2[1 4rr4y, =ith 4rr4y[0. ,ro- the ,irst ele-ent (4), 4nd 4rr4y[1. ,ro- the second (')
yield+ll r4nge(G) 3in 4 ,6nction, repe4tedly yields
cat([1,2,3.,[G,L,J,H.) 3conc4ten4tes t=o seA6ences
yo6rN4-e F ets() 3does 4 Console:Re4d0ine()
yo6rN4-e F prompt(%$nter yo6r n4-e %)
i-port Syste-:I!> e F StreamIriter(2test:d4t2)> e:IriteLine(2This is the ,irst line:2)> e:Close()
usin o8@ectsCre4tion7 36sing =ill ens6re the o8@ects (eg ,ile h4ndles) in the ,ollo=ing code 8locB 4re closed5disposed o,
s!ell(,ileN4-eStr, 4rg6-entStr) 3r6ns the 4pplic4tion ,ileN4-eStr, =ith 4rg6-ents 4rg6-entStr> o6tp6t is to console
s!ellm(,ileN4-eStr, 4rg6-entStr'rr) 4s 48o9e =ith 4rg6-entStr'rr 4s 4n 4rr4y o, 4rg6-ent strings> o6tp6t is ret6rned
s!ellp(,ileN4-eStr, 4rg6-entStr) st4rts 4 process 4nd ret6rns 4 process o8@ect
lock so-e!8@ect7 in 4 -6lti<thre4ded en9iron-ent, pre9ents si-6lt4neo6s 6se o, 4n o8@ect =ithin the locB code 8locB
assert Slogic4l$+pressionR 3eA6i94lent to76nless(Slogic4l$+pressionR)7
r4ise Boo:'ssertion/4iled$+ception(%(Slogic4l$+pressionR)%)
preservin 94r1, 94r27 3restores the 94l6es o, the 94ri48les listed, =hen the ,ollo=ing code 8locB e+its
macro de,46ltI-ports7 yield [] i-port Syste- ].> yield [] i-port Syste-:Collections:)eneric ]. 3de,ining 4 -4cro
de,46ltI-ports> Console:1rite0ine(typeo,(0ist[o,;.):N4-esp4ce) 3in9oBing this -4cro
Syste-:)nvironment 3is 4 st4tic cl4ss =ith 4 plethor4 o, properties D -ethods
(J)E Base Class Library&s rich set o, types, inter,4ces, cl4sses, properties, 4nd -ethods is 494il48le to Boo progr4-s
+ttributes 4dd -et4<in,or-4tion to the code
[e,46lt#e-8er(2Ite-2).
[collection(string). 3 in ,ront o, cl4ss StringCollection()
[!8solete.
['ttri86te"s4ge('ttri86teT4rgets:#ethod).
['ttri86te"s4ge('ttri86teT4rgets:'ll).
[Si-ple.
[9ol4tile.
[Syste-:/l4gs.
[Syste-:R6nti-e:Co-pilerSer9ices:Co-piler)lo84lScope.
[/l4gs.
[4sse-8ly7 'sse-8lyTitle(%,oo%).
[4sse-8ly7 'sse-8lyescription(%84r%).
[4sse-8ly7 Strict#ode.

You might also like