You are on page 1of 34

C# FAQ

Contents
1. Introduction o 1.1 What is C#? o 1.2 How do I develop C# apps? o 1.3 Where can I download the .NET S ! " #isual Studio.NET? o 1.$ oes C# replace %ava? o 1.& oes C# replace C''? o 1.( What does a si)ple C# pro*ra) loo+ li+e? o 1., Is C# o-.ect/oriented? o 1.0 oes C# have its own class li-rar1? 2. 2asic t1pes o 2.1 What standard t1pes does C# suppl1? o 2.2 Is it true that all C# t1pes derive 3ro) a co))on -ase class? o 2.3 So this )eans I can pass an instance o3 a value t1pe to a )ethodthat ta+es an o-.ect as a para)eter? o 2.$ What are the 3unda)ental di33erences -etween value t1pes andre3erence t1pes? o 2.& 4+a15 so an int is a value t1pe5 and a class is are3erence t1pe. How can int -e derived 3ro) object? o 2.( C# uses re3erences instead o3 pointers. 6re C# re3erences the sa)eas C'' re3erences? 3. Classes and structs o 3.1 Structs are lar*el1 redundant in C''. Wh1 does C# have the)? o 3.2 oes C# support )ultiple inheritance 78I9? o 3.3 Is a C# inter3ace the sa)e as a C'' a-stract class? o 3.$ 6re C# constructors the sa)e as C'' constructors? o 3.& 6re C# destructors the sa)e as C'' destructors? o 3.( I3 C# destructors are so di33erent to C'' destructors5 wh1 did 8Suse the sa)e s1nta:? o 3., What is a static constructor? o 3.0 6re all )ethods virtual in C#? o 3.; How do I declare a pure virtual 3unction in C#? $. E:ceptions o $.1 Can I use e:ceptions in C#? o $.2 What t1pes o3 o-.ect can I throw as e:ceptions? o $.3 Can I de3ine )1 own e:ceptions? o $.$ 6re there an1 standard e:ceptions that I can re/use? o $.& oes the S1ste).E:ception class have an1 cool 3eatures? o $.( When should I throw an e:ception? o $., oes C# have a <throws< clause? &. =un/ti)e t1pe in3or)ation o &.1 How can I chec+ the t1pe o3 an o-.ect at runti)e? o &.2 Can I *et the na)e o3 a t1pe at runti)e? (. 6dvanced lan*ua*e 3eatures o (.1 What are dele*ates? o (.2 6re dele*ates .ust li+e inter3aces with a sin*le )ethod? o (.3 What is the C# e>uivalent o3 ?uer1Inter3ace? ,. It doesn<t wor+ li+e that in C'' ... o ,.1 I <new</ed an o-.ect5 -ut how do I delete it? o ,.2 I tried to create an o-.ect on the stac+5 -ut the C# co)pilerco)plained. What<s *oin* on? o ,.3 I de3ined a destructor5 -ut it never *ets called. Wh1? o ,.$ 8ost o3 the C# -asic t1pes have the sa)e na)es as C'' -asic t1pes?6re the1 the sa)e? 0. 8iscellaneous

o o o o o o

0.1 Strin* co)parisons usin* @@ see) to -e case/sensitive? How do I doa case/insensitive strin* co)parison? 0.2 I<ve seen so)e strin* literals which use the A s1)-ol5 and so)ewhich don<t. What<s that all a-out? 0.3 oes C# support a varia-le nu)-er o3 ar*u)ents? 0.$ How can I process co))and/line ar*u)ents? 0.& oes C# do arra1 -ounds chec+in*? 0.( How can I )a+e sure )1 C# classes will interoperate with other .NETlan*ua*es?

;. =esources o ;.1 =eco))ended -oo+s o ;.2 Internet =esources o ;.3 Sa)ple code and utilities o

1. Introduction
1.1 What is C#? C# is a pro*ra))in* lan*ua*e desi*ned -1 8icroso3t. It is loosel1 -ased on CBC''5 and -ears a stri+in* si)ilarit1 to %ava in )an1 wa1s. 8icroso3t descri-e C# as 3ollowsC DC# is a si)ple5 )odern5 o-.ect oriented5 and t1pe/sa3e pro*ra))in* lan*ua*e derived 3ro) C and C''. C# 7pronounced <C sharp<9 is 3ir)l1 planted in the C and C'' 3a)il1 tree o3 lan*ua*es5 and will i))ediatel1 -e 3a)iliar to C and C'' pro*ra))ers. C# ai)s to co)-ine the hi*h productivit1 o3 #isual 2asic and the raw power o3 C''.D 1.2 How do I develop C# apps? The 73ree9 .NET S ! contains the C# co))and/line co)piler 7csc.e:e9. #isual Studio.NET has 3ull1 inte*rated support 3or C# develop)ent. 1.3 Where can I download the .N ! "#$ % &isual "tudio.N !? Eou can download the S ! 3ro) httpCBBwww.)icroso3t.co)Bnet. I3 1ou are an 8S N Fniversal su-scri-er5 1ou can also download #isual Studio.NET. 1.' #oes C# replace (ava? C# is a ver1 %ava/li+e lan*ua*e / the core o3 -oth lan*ua*es have si)ilar advanta*es and li)itations when co)pared to C''. Gor e:a)ple5 -oth lan*ua*es have *ar-a*e collection5 -ut neither lan*ua*e has te)plates. 8icroso3t have ceased production o3 #isual %''5 so it<s hard not to view C# as 8icroso3t<s alternative to %ava. 1.) #oes C# replace C**? The o-vious answer is no. However it<s di33icult to see C'' as the -est choice 3or new .NET code. Gor the .NET runti)e to 3unction 3ull15 it re>uires the pro*ra))in* lan*ua*e to con3or) to certain rules / one o3 these rules is that lan*ua*e t1pes )ust con3or) to the Co))on T1pe S1ste) 7CTS9. Fn3ortunatel1 )an1 C'' 3eatures are not supported -1 the CTS / 3or e:a)ple )ultiple inheritance o3 classes and te)plates. 8icroso3t<s answer to this pro-le) is to o33er 8ana*ed E:tensions 78E9 3or C''5 which allows 1ou to write C'' that con3or)s to the CTS. New +e1words are provided to )ar+ 1our C'' classes with CTS attri-utes 7e.*. HH*c 3or *ar-a*e collection9. However5 it<s di33icult to see wh1 8E C'' would -e chosen over C# 3or new pro.ects. In ter)s o3 3eatures the1 are ver1 si)ilar5 -ut unli+e C''5 C# has -een desi*ned 3ro) the *round/up to wor+ sea)lessl1 with the .NET environ)ent. The raison d<etre 3or 8E C'' would there3ore appear to -e portin* e:istin* C'' code to the .NET environ)ent. So5 in answer to the >uestion5 )1 suspicion is that C'' will re)ain an i)portant lan*ua*e outside o3 the .NET environ)ent5 and will -e used 7via 8E9 to port e:istin* code to .NET5 -ut I thin+ C# will -eco)e the lan*ua*e o3 choice 3or one/ti)e C'' developers developin* new .NET applications. 2ut onl1 ti)e will tell ... 1.+ What does a si,ple C# pro-ra, loo. li.e? Sorr15 )1 i)a*ination has deserted )e. Ees5 1ou *uessed it5 here co)es <Hello5 world< ... class C6pplication I pu-lic static void 8ain79 I S1ste).Console.Write7 DHello5 new .NET world.D 9J K K 7No5 1ou can<t put 8ain79 as a *lo-al 3unction / *lo-al 3unctions don<t e:ist in C#.9

1./ Is C# o01ect2oriented? Ees5 C# is an 44 lan*ua*e in the tradition o3 %ava and C''. 1.3 #oes C# have its own class li0rar4? Not e:actl1. In co))on with all .NET lan*ua*es 7e.*. #isual2asic.NET5 %Script.NET9 C# has access to the .NET class li-rar1. C# does not have its own class li-rar1.

2. 5asic t4pes
2.1 What standard t4pes does C# suppl4? C# supports a ver1 si)ilar ran*e o3 -asic t1pes to C''5 includin* int5 lon*5 3loat5 dou-le5 char5 strin*5 arra1s5 structs and classes. However5 don<t assu)e too )uch. The na)es )a1 -e 3a)iliar5 -ut so)e o3 the details are di33erent. Gor e:a)ple5 a lon* is ($ -its in C#5 whereas in C'' the siLe o3 a lon* depends on the plat3or) 7t1picall1 32 -its on a 32/ -it plat3or)5 ($ -its on a ($/-it plat3or)9. 6lso classes and structs are al)ost the sa)e in C'' / this is not true 3or C#. 2.2 Is it true that all C# t4pes derive 6ro, a co,,on 0ase class? Ees and no. 6ll t1pes can -e treated as i3 the1 derive 3ro) o01ect 7S1ste).4-.ect95 -ut in order to treat an instance o3 a value t1pe 7e.*. int5 3loat9 as o01ect/derived5 the instance )ust -e converted to a re3erence t1pe usin* a process called <-o:in*<. In theor1 a developer can 3or*et a-out this and let the run/ti)e worr1 a-out when the conversion is necessar15 -ut in realit1 this i)plicit conversion can have side/e33ects that )a1 trip up the unwar1. 2.3 "o this ,eans I can pass an instance o6 a value t4pe to a ,ethodthat ta.es an o01ect as a para,eter? Ees. Gor e:a)pleC class C6pplication I pu-lic static void 8ain79 I int : @ 2&J strin* s @ D3redDJ ispla18e7 : 9J ispla18e7 s 9J K static void ispla18e7 o-.ect o 9 I S1ste).Console.WriteMine7 DEou are INKD5 o 9J K K This would displa1C Eou are 2& Eou are 3red 2.' What are the 6unda,ental di66erences 0etween value t4pes andre6erence t4pes? C# divides t1pes into two cate*ories / value t1pes and reference t1pes. 8ost o3 the -asic intrinsic t1pes 7e.*. int5 char9 are value t1pes. Structs are also value t1pes. =e3erence t1pes include classes5 inter3aces5 arra1s and strin*s. The -asic idea is strai*ht3orward / an instance o3 a value t1pe represents the actual data 7stored on the stac+95 whereas an instance o3 a re3erence t1pe represents a pointer or re3erence to the data 7stored on the heap9. The )ost con3usin* aspect o3 this 3or C'' developers is that C# has predeter)ined which t1pes will -e represented as values5 and which will -e represented as re3erences. 6 C'' developer e:pects to ta+e responsi-ilit1 3or this decision. Gor e:a)ple5 in C'' we can do thisC int :1 @ 3J BB :1 is a value on the stac+ int O:2 @ new int739 BB :2 is a re3erence to a value on the heap -ut in C# there is no controlC int :1 @ 3J BB :1 is a value on the stac+ int :2 @ new int79J :2 @ 3J BB :2 is also a value on the stac+P 2.) 7.a48 so an int is a value t4pe8 and a class is are6erence t4pe. How can int 0e derived 6ro, object? It isn<t5 reall1. When an int is -ein* used as an int5 it is a value 7on the stac+9. However5 when it is -ein* used as an o01ect5 it is a re3erence to an inte*er value on the heap. In other words5 when 1ou treat an int as an o-.ect5 the

runti)e auto)aticall1 converts the int value to an o01ect re3erence. This process is called -o:in*. The conversion involves cop1in* the contents o3 the int 3ro) the stac+ to the heap5 and creatin* an o01ect instance which re3ers to it. Fn-o:in* is the reverse process / the o-.ect is converted -ac+ to a stac+/-ased value. int : @ 3J BB new int value 3 on the stac+ o-.ect o-.: @ :J BB new int on heap5 set to value 3 / still have :@3 on stac+ int 1 @ 7int9o-.:J BB new value 3 on stac+5 still *ot :@3 on stac+ and o-.:@3 on heap 2.+ C# uses re6erences instead o6 pointers. Are C# re6erences the sa,eas C** re6erences? Not >uite. The -asic idea is the sa)e5 -ut one si*ni3icant di33erence is that C# re3erences can -e null . So 1ou cannot rel1 on a C# re3erence pointin* to a valid o-.ect. I3 1ou tr1 to use a null re3erence5 a Null=e3erenceE:ception is thrown. Gor e:a)ple5 loo+ at the 3ollowin* )ethodC void displa1Strin*Men*th7 strin* s 9 I Console.WriteMine7 DStrin* is len*th INKD5 s.Men*th 9J K The pro-le) with this )ethod is that it will throw a Null=e3erenceE:ception i3 called li+e thisC strin* s @ nullJ displa1Strin*Men*th7 s 9J 43 course 3or so)e situations 1ou )a1 dee) a Null=e3erenceE:ception to -e a per3ectl1 accepta-le outco)e5 -ut in this case it )i*ht -e -etter to re/write the )ethod li+e thisC void displa1Strin*Men*th7 strin* s 9 I i37 s @@ null 9 Console.WriteMine7 DStrin* is nullD 9J else Console.WriteMine7 DStrin* is len*th INKD5 s.Men*th 9J K

3. Classes and structs


3.1 "tructs are lar-el4 redundant in C**. Wh4 does C# have the,? In C''5 a struct and a class are prett1 )uch the sa)e thin*. The onl1 di33erence is the de3ault visi-ilit1 level 7pu-lic 3or structs5 private 3or classes9. However5 In C# structs and classes are ver1 di33erent. In C#5 structs are value t1pes 7stored on the stac+95 whereas classes are reference t1pes 7stored on the heap9. 6lso structs cannot inherit 3ro) structs or classes5 thou*h the1 can i)ple)ent inter3aces. Structs cannot have destructors. 3.2 #oes C# support ,ultiple inheritance 9:I;? C# supports )ultiple inheritance o3 interfaces5 -ut not o3 classes. 3.3 Is a C# inter6ace the sa,e as a C** a0stract class? No5 not >uite. 6n a-stract class in C'' cannot -e instantiated5 -ut it can 7and o3ten does9 contain i)ple)entation code andBor data )e)-ers. 6 C# inter3ace cannot contain an1 i)ple)entation code or data )e)-ers / it is si)pl1 a *roup o3 )ethod na)es " si*natures. 6 C# inter3ace is )ore li+e a C48 inter3ace than a C'' a-stract class. The other )a.or di33erence is that a C# class can inherit 3ro) onl1 one class 7a-stract or not95 -ut can i)ple)ent )ultiple inter3aces. 3.' Are C# constructors the sa,e as C** constructors? #er1 si)ilar. 3.) Are C# destructors the sa,e as C** destructors? NoP The1 loo+ the sa)e -ut the1<re ver1 di33erent. Girst o3 all5 a C# destructor isn<t *uaranteed to -e called at an1 particular ti)e. In 3act it<s not *uaranteed to -e called at all. Truth -e told5 a C# destructor is reall1 .ust a GinaliLe )ethod in dis*uise. In particular5 it is a GinaliLe )ethod with a call to the -ase class GinaliLe )ethod inserted. So thisC class CTest I QCTest79 I S1ste).Console.WriteMine7 D21e -1eD 9J K K is reall1 thisC

class CTest I protected override void GinaliLe79 I S1ste).Console.WriteMine7 D21e -1eD 9J -ase.GinaliLe79J K K With the arrival o3 2eta 25 e:plicitl1 overridin* GinaliLe79 li+e this is not allowed / the destructor s1nta: )ust -e used. 3.+ I6 C# destructors are so di66erent to C** destructors8 wh4 did :"use the sa,e s4nta<? 2ecause the1<re evil5 and the1 want to )ess with 1our )ind. 3./ What is a static constructor? 6 constructor 3or a class5 rather than instances o3 a class. The static constructor is called when the class is loaded. 3.3 Are all ,ethods virtual in C#? No. Mi+e C''5 )ethods are non/virtual -1 de3ault5 -ut can -e )ar+ed as virtual. 3.= How do I declare a pure virtual 6unction in C#? Fse the a-stract )odi3ier on the )ethod. The class )ust also -e )ar+ed as a-stract 7naturall19. Note that a-stract )ethods cannot have an i)ple)entation 7unli+e pure virtual C'' )ethods9.

'. <ceptions
'.1 Can I use e<ceptions in C#? Ees5 in 3act e:ceptions are the reco))ended error/handlin* )echanis) in C# 7and in .NET in *eneral9. 8ost o3 the .NET 3ra)ewor+ classes use e:ceptions to si*nal errors. '.2 What t4pes o6 o01ect can I throw as e<ceptions? 4nl1 instances o3 the S1ste).E:ception classes5 or classes derived 3ro) S1ste).E:ception. This is in sharp contrast with C'' where instances o3 al)ost an1 t1pe can -e thrown. '.3 Can I de6ine ,4 own e<ceptions? Ees5 as lon* as 1ou 3ollow the rule that e:ceptions derive 3ro) S1ste).E:ception. 8ore speci3icall15 8S reco))end that user/de3ined e:ceptions inherit 3ro) S1ste).6pplicationE:ception 7which is derived 3ro) S1ste).E:ception9. '.' Are there an4 standard e<ceptions that I can re2use? Ees5 and so)e o3 the) loosel1 correspond to standard C48 H=ESFMTs. The ta-le -elow shows a )appin* 3ro) H=ESFMTs to .NET 7and there3ore C#9 e:ceptionsC H> "?@! EHIN#6MI 6=R .N ! AC B!I7N ArgumentOutOfRangeException5 or the )ore *eneral ArgumentException. 6lternativel1 FormatException i3 a supplied para)eter is not the correct 3or)at / e.*. a F=M is speci3ied as htpCBB instead o3 httpCBB ArgumentNullException NotImplementedException So)e )a1 consider InvalidOperationException to -e e>uivalent. InvalidOperationException is nor)all1 used to indicate that an o-.ect cannot per3or) the re>uested operation -ecause it is not in a suita-le state to do so. OutOfMemoryException

EHS4INTE= EHN4TI8SM EHFNETSECTE

EH4FT4G8E84=E

4ther standard e:ceptions that 1ou )i*ht want to re/use are IndexOutOfRangeException and ArithmeticException . '.) #oes the "4ste,. <ception class have an4 cool 6eatures? Ees / the 3eature which stands out is the Stac+Trace propert1. This provides a call stac+ which records where the e:ception was thrown 3ro). Gor e:a)ple5 the 3ollowin* codeC

usin* S1ste)J class C6pp I pu-lic static void 8ain79 I tr1 I 379J K catch7 E:ception e 9 I Console.WriteMine7 DS1ste).E:ception stac+ trace @ UnINKD5 e.Stac+Trace 9J K K static void 379 I throw new E:ception7 D3 went pear/shapedD 9J K K produces this outputC S1ste).E:ception stac+ trace @ at C6pp.379 at C6pp.8ain79 Note5 however5 that this stac+ trace was produced 3ro) a de-u* -uild. 6 release -uild )a1 opti)ise awa1 so)e o3 the )ethod calls which could )ean that the call stac+ isn<t >uite what 1ou e:pect. '.+ When should I throw an e<ception? This is the su-.ect o3 so)e de-ate5 and is partl1 a )atter o3 taste. However5 it is accepted -1 )an1 that e:ceptions should -e thrown onl1 when an <une:pected< error occurs. How do 1ou decide i3 an error is e:pected or une:pected? This is a .ud*e)ent call5 -ut a strai*ht3orward e:a)ple o3 an e:pected error is 3ailin* to read 3ro) a 3ile -ecause the see+ pointer is at the end o3 the 3ile5 whereas an e:a)ple o3 an une:pected error is 3ailin* to allocate )e)or1 3ro) the heap. './ #oes C# have a CthrowsC clause? No5 unli+e %ava5 C# does not re>uire 7or even allow9 the developer to speci31 the e:ceptions that a )ethod can throw.

). >un2ti,e t4pe in6or,ation


).1 How can I chec. the t4pe o6 an o01ect at runti,e? Eou can use the is +e1word. Gor e:a)pleC usin* S1ste)J class C6pp I pu-lic static void 8ain79 I strin* s @ D3redDJ lon* i @ 1NJ Console.WriteMine7 DINK is I1Kan inte*erD5 s5 7IsInte*er7s9 ? DD C Dnot D9 9J Console.WriteMine7 DINK is I1Kan inte*erD5 i5 7IsInte*er7i9 ? DD C Dnot D9 9J K static -ool IsInte*er7 o-.ect o-. 9 I i37 o-. is int VV o-. is lon* 9 return trueJ else return 3alseJ K K

produces the outputC 3red is not an inte*er 1N is an inte*er ).2 Can I -et the na,e o6 a t4pe at runti,e? Ees5 use the RetT1pe )ethod o3 the o-.ect class 7which all t1pes inherit 3ro)9. Gor e:a)pleC usin* S1ste)J class CTest I class C6pp I pu-lic static void 8ain79 I lon* i @ 1NJ CTest ctest @ new CTest79J ispla1T1peIn3o7 ctest 9J ispla1T1peIn3o7 i 9J K static void ispla1T1peIn3o7 o-.ect o-. 9 I Console.WriteMine7 DT1pe na)e @ INK5 3ull t1pe na)e @ I1KD5 o-..RetT1pe795 o-..RetT1pe79.GullNa)e 9J K K K produces the 3ollowin* outputC T1pe na)e @ CTest5 3ull t1pe na)e @ CTest T1pe na)e @ Int($5 3ull t1pe na)e @ S1ste).Int($

+. Advanced lan-ua-e 6eatures


+.1 What are dele-ates? 6 dele*ate is a class derived 3ro) S1ste). ele*ate. However the lan*ua*e has a special s1nta: 3or declarin* dele*ates which )eans that the1 don<t loo+ li+e classes. 6 dele*ate represents a )ethod with a particular si*nature. 6n instance o3 a dele*ate represents a )ethod with a particular si*nature on a particular o-.ect 7or class in the case o3 a static )ethod9. Gor e:a)pleC usin* S1ste)J dele*ate void Stereot1pe79J class C6)erican I pu-lic void 2eSatriotic79 I Console.WriteMine7 D... W*ulpX ... Rod -less 6)erica.D9J K K class C2rit I pu-lic void 2eTenopho-ic79 I Console.WriteMine7 D2lood1 3orei*ners ... D 9J K K class C6pplication I pu-lic static void =evealEourStereot1pe7 Stereot1peYZ stereot1pes 9 I

3oreach7 Stereot1pe s in stereot1pes 9 s79J K pu-lic static void 8ain79 I C6)erican chuc+ @ new C6)erican79J C2rit edward @ new C2rit79J BB Create our list o3 sterot1pes. Stereot1peYZ stereot1pes @ new Stereot1peY2ZJ stereot1pesYNZ @ new Stereot1pe7 chuc+.2eSatriotic 9J stereot1pesY1Z @ new Stereot1pe7 edward.2eTenopho-ic 9J BB =eveal 1ourselvesP =evealEourStereot1pe7stereot1pes 9J K K This produces the 3ollowin* resultC ... W*ulpX... Rod -less 6)erica. 2lood1 3orei*ners ... +.2 Are dele-ates 1ust li.e inter6aces with a sin-le ,ethod? Conceptuall1 dele*ates can -e used in a si)ilar wa1 to an inter3ace with a sin*le )ethod. The )ain practical di33erence is that with an inter3ace the )ethod na)e is 3i:ed5 whereas with a dele*ate onl1 the si*nature is 3i:ed / the )ethod na)e can -e di33erent5 as shown in the e:a)ple a-ove. +.3 What is the C# eDuivalent o6 Quer4Inter6ace? The as +e1word. Gor e:a)pleC usin* S1ste)J inter3ace ISerson I strin* RetNa)e79J K inter3ace ISerson2 C ISerson I int Ret6*e79J K class CSerson C ISerson I pu-lic CSerson7 strin* na)e 9 I )Hna)e @ na)eJ K BB ISerson pu-lic strin* RetNa)e79 I return )Hna)eJ K private strin* )Hna)eJ K class CSerson2 C ISerson2 I pu-lic CSerson27 strin* na)e5 int a*e 9 I )Hna)e @ na)eJ

)Ha*e @ a*eJ K BB ISerson2 pu-lic strin* RetNa)e79 I return )Hna)eJ K pu-lic int Ret6*e79 I return )Ha*eJ K private strin* )Hna)eJ private int )Ha*eJ K pu-lic class C6pp I pu-lic static void 8ain79 I CSerson -o- @ new CSerson7 D2o-D 9J CSerson2 sheila @ new CSerson27 DSheilaD5 2$ 9J ispla16*e7 -o- 9J ispla16*e7 sheila 9J K static void ispla16*e7 ISerson person 9 I ISerson2 person2 @ person as ISerson2J BB ?uer1Inter3ace lives on PPP i37 person2 P@ null 9 Console.WriteMine7 DINK is I1K 1ears old.D5 person2.RetNa)e795 person2.Ret6*e79 9J else Console.WriteMine7 DSorr15 don<t +now INK<s a*e.D5 person.RetNa)e79 9J K K =unnin* the pro*ra) produces the 3ollowin* outputC Sorr15 don<t +now 2o-<s a*e. Sheila is 2$ 1ears old.

/. It doesnCt wor. li.e that in C** ...


/.1 I CnewC2ed an o01ect8 0ut how do I delete it? Eou can<t. Eou are not allowed to call the destructor e:plicitl15 and no delete operator is provided. on<t worr15 the *ar-a*e collector will destro1 1our o-.ect .... eventuall1 .... pro-a-l1 .... C/9 /.2 I tried to create an o01ect on the stac.8 0ut the C# co,pilerco,plained. WhatCs -oin- on? Fnli+e C''5 1ou cannot create instances o3 classes on the stac+. Class instances alwa1s live on the heap and are )ana*ed -1 the *ar-a*e collector. /.3 I de6ined a destructor8 0ut it never -ets called. Wh4? 6 C# destructor is reall1 .ust an i)ple)entation o3 GinaliLe5 and the runti)e doesn<t *uarantee to call GinaliLe )ethods. The se)antics o3 a C# destructor are >uite di33erent 3ro) a C'' destructor. /.' :ost o6 the C# 0asic t4pes have the sa,e na,es as C** 0asic t4pes?Are the4 the sa,e? No. 6 char in C# is e>uivalent to a wcharHt in C''. 6ll characters 7and strin*s5 o-viousl19 are Fnicode in C#. Inte*ral values in C# are concrete siLes5 unli+e C'' 7where siLe depends on processor9. Gor e:a)ple5 a C# int is 32 -its5 whereas a C'' int is nor)all1 32 -its on a 32/-it processor and ($ -its on a ($/-it processor. 6 C# lon* is ($ -its.

3. :iscellaneous
3.1 "trin- co,parisons usin- EE see, to 0e case2sensitive? How do I doa case2insensitive strin- co,parison? Fse the Strin*.Co)pare 3unction. Its third para)eter is a -oolean which speci3ies whether case should -e i*nored or not. D3redD @@ DGredD BB 3alse S1ste).Strin*.Co)pare7 D3redD5 DGredD5 true 9 BB true 3.2 ICve seen so,e strin- literals which use the F s4,0ol8 and so,ewhich donCt. WhatCs that all a0out? The A s1)-ol -e3ore a strin* literal )eans that escape se>uences are i*nored. This is particularl1 use3ul 3or 3ile na)es5 e.*.

strin* 3ileNa)e @ DcCUUte)pUUtest.t:tD versusC strin* 3ileNa)e @ ADcCUte)pUtest.t:tD 3.3 #oes C# support a varia0le nu,0er o6 ar-u,ents? Ees5 usin* the params +e1word. The ar*u)ents are speci3ied as a list o3 ar*u)ents o3 a speci3ic t1pe5 e.*. int. Gor ulti)ate 3le:i-ilit15 the t1pe can -e object. The standard e:a)ple o3 a )ethod which uses this approach is S1ste).Console.WriteMine79. 3.' How can I process co,,and2line ar-u,ents? Mi+e thisC usin* S1ste)J class C6pp I pu-lic static void 8ain7 strin*YZ ar*s 9 I Console.WriteMine7 DEou passed the 3ollowin* ar*u)entsCD 9J 3oreach7 strin* ar* in ar*s 9 Console.WriteMine7 ar* 9J K K 3.) #oes C# do arra4 0ounds chec.in-? Ees. 6n IndexOutOfRange e:ception is used to si*nal an error. 3.+ How can I ,a.e sure ,4 C# classes will interoperate with other .N !lan-ua-es? 8a+e sure 1our C# code con3or)s to the Co))on Man*ua*e Su-set 7CMS9. To help with this5 add the Yasse)-l1CCMSCo)pliant7true9Z *lo-al attri-ute to 1our C# source 3iles. The co)piler will e)it an error i3 1ou use a C# 3eature which is not CMS/co)pliant.

#ele-ates
QG What do dele-ates 0u4 4ou? 6C ele*ates ena-le scenarios that so)e other lan*ua*es have addressed with 3unction pointers. However5 unli+e 3unction pointers5 dele*ates are o-.ect/oriented and t1pe/sa3e. QG Are dele-ates reall4 t4pe2sa6e? Not reall1. 6 dele*ate instance does not +now or care a-out the classes o3 the )ethods it encapsulatesJ all that )atters is that those )ethods -e co)pati-le with the dele*ate<s t1pe. QG What does that ,ean? The pro-le) is -est illustrated with an e:a)pleC

usin* S1ste)J class epart)ent I pu-lic dele*ate void Girea-le79J pu-lic static void GireE)plo1ee7Girea-le 3irea-le9 I 3irea-le79J K K class 8ilitar1 I pu-lic dele*ate void Girea-le79J pu-lic static void Gire8issile7Girea-le 3irea-le9 I 3irea-le79J K K class 8issile I pu-lic void Gire79 I Console.WriteMine7D)issile 3iredD9J K K class E)plo1ee I pu-lic void Gire79 I Console.WriteMine7De)plo1ee 3iredD9J K K class Test I static E)plo1ee e1 @ new E)plo1ee79J static 8issile e2 @ new 8issile79J static void 8ain7strin*YZ ar*s9 I epart)ent.Girea-le e @ new epart)ent.Girea-le7e2.Gire9J epart)ent.GireE)plo1ee7e9J K K error. The line epart)ent.Girea-le e @ new epart)ent.Girea-le7e2.Gire9J epart)ent.Girea-le e @ new epart)ent.Girea-le7e1.Gire9J In this pro*ra)5 the pro*ra))er has )ade an reall1 should -e

However the co)piler does not detect this codin* error. The pro*ra) co)piles 3ine and 1ou *et this output when 1ou run itC )issile 3ired Fh ohP The pro*ra))er intended to 3ire an e)plo1ee5 -ut he has launched a )issile insteadP QG Is there a 0etter wa4 to do this? 6C Ees. Eou can *et t1pe sa3et1 -1 usin* inter3aces instead o3 dele*ates.

usin* S1ste)J class epart)ent I pu-lic inter3ace Girea-le I void Gire79J K pu-lic static void GireE)plo1ee7Girea-le 3irea-le9 I 3irea-le.Gire79J K K class 8ilitar1 I pu-lic inter3ace Girea-le I void Gire79J K pu-lic static void Gire8issile7Girea-le 3irea-le9 I 3irea-le.Gire79J K K class 8issile C 8ilitar1.Girea-le I pu-lic void Gire79 I Console.WriteMine7D)issile 3iredD9J K K class E)plo1ee C epart)ent.Girea-le I pu-lic void Gire79 I Console.WriteMine7De)plo1ee 3iredD9J K K class Test I static E)plo1ee e1 @ new E)plo1ee79J static 8issile e2 @ new 8issile79J static void 8ain7strin*YZ ar*s9 I epart)ent.Girea-le e @ e1J epart)ent.GireE)plo1ee7e9J K K epart)ent.Girea-le e @ e1J epart)ent.Girea-le e @ e2J to thisC Tr1 chan*in* the line

When 1ou tr1 to co)pile the pro*ra) the co)piler tells 1ouC test.cs7305339C error CSNN2;C Cannot i)plicitl1 convert t1pe <8issile< to < epart)ent.Girea-le< The co)piler has detected 1our codin* errorP QG What is t4pe sa6et4 all a0out? 6C T1pe sa3et1 is a-out increasin* the opportunities 3or the co)piler to detect 1our codin* errors. I3 1ou use inter3aces instead o3 dele*ates the co)piler will have )ore opportunities to detect 1our codin* errors. QG What other di66erences e<ist 0etween dele-ates and inter6aces? 6C Inter3aces carr1 se)antics5 and when a pro*ra))er i)ple)ents an inter3ace5 he is t1picall1 well aware o3 that se)antics. When 1ou tr1 to invo+e a particular )ethod via an inter3ace5 1ou can -e 3airl1 certain that i3 1ou succeed5 the se)antics o3 that )ethod is what 1ou e:pect. Gor that reason5 usin* inter3aces is essentiall1 doin* a chec+ 3or se)antic correctness on so)e level. ele*ates5 on the other hand5 -1 onl1 veri31in* the )ethod si*nature5 )a+e the pro*ra))er responsi-le 3or ensurin* that the se)antics o3 the )ethod is co)pati-le. The se)antics )a1 cover not onl1 the )eanin* o3 the ar*u)ents and return value 7so)e ti)es even the order o3 the ar*u)ents i3 the1 are o3 the sa)e t1pe95 the ran*es o3 the ar*u)ents5 -ut also an invocation order when )ultiple )ethods are concerned. Hence5 in a su33icientl1 lar*e pro*ra) there is plent1 o3 )ar*in to )a+e an error when di33erent pro*ra))ers are not 3orced to co)pl1 with a uni3or) se)antics 7as the1 would -e i3

inter3aces were used9. YcreditC 8ind 2rid*eZ QG What can we conclude 6ro, all this? 6C Fse o3 dele*ates results in shorter -ut less relia-le code.

5o<in- and un0o<inQG What does 0o<in- and un0o<in- 0u4 4ou? 6C 2o:in* and un-o:in* ena-le type system unification. QG What is t4pe s4ste, uni6ication? The *oal o3 t1pe s1ste) uni3ication is to -rid*e the *ap -etween value t1pes and re3erence t1pes that e:ists in )ost lan*ua*es. Gor e:a)ple5 a Stac+ class can provide Sush and Sop )ethods that ta+e and return o-.ect values. pu-lic class Stac+ I pu-lic o-.ect Sop79 I...K pu-lic void Sush7o-.ect o9 I...K K 2ecause C# has a uni3ied t1pe s1ste)5 the Stac+ class can -e used with ele)ents o3 an1 t1pe5 includin* value t1pes li+e int. QG #oes 0o<in- and un0o<in- reall4 0rid-e the -ap 0etween value t4pes and re6erence t4pes? No5 not reall1. =e3erence t1pes have the concept o3 identit1 and e>ualit15 while value t1pes onl1 have e>ualit1. 2o:in* and un-o:in* does not chan*e this 3unda)ental di33erence. 6uto-o:in* in C# allows value t1pes to *o -ac+ and 3orth -etween -ein* o-.ects5 -ut each ti)e a value t1pe -eco)es an o-.ect it ac>uires a new identit1. Here<s a sa)ple pro*ra) to illustrate this pro-le)C usin* S1ste)J struct 6 I K class 2 I K pu-lic class Test I static 4-.ect Srocess74-.ect o9 I i3 7o is 69 I 6 a @ 769oJ BB do so)ethin* with a return aJ K else i3 7o is 29 I 2 - @ 729oJ BB do so)ethin* with return -J K return nullJ K static void Chec+74-.ect o9 I Console.WriteMine7o @@ Srocess7o99J K pu-lic static void 8ain7strin*YZ ar*s9 I Chec+7new 6799J Chec+7new 2799J K K This pro*ra) prints Galse True

which )a1 -e une:pected i3 1ou don<t understand that value t1pes and re3erence t1pes cannot -e treated the sa)e wa1. This illustrates the 3act that C# does not -rid*e the *ap -etween value t1pes and re3erence t1pes. The solution to this pro-le) is to use wrapper classes. Sretendin* there is no di33erence -etween re3erence t1pes and value t1pes is dan*erous as it will invaria-l1 result in hard/to/detect -u*s5 as de)onstrated -1 this e:a)ple. ?G What other pit6alls should I 0e aware o6? 6C 6 -o:in* conversion alwa1s )a+es a cop1 o3 the value -ein* -o:ed. This is di33erent 3ro) a conversion o3 a re3erence/t1pe to t1pe o-.ect5 in which the value continues to re3erence the sa)e instance. Gor e:a)ple5 *iven the declaration struct Soint I pu-lic int :5 1J pu-lic Soint7int :5 int 19 I this.: @ :J this.1 @ 1J K K the 3ollowin* state)ents Soint p @ new Soint71N5 1N9J o-.ect -o: @ pJ p.: @ 2NJ Console.Write777Soint9-o:9.:9J will output the value 1N -ecause the i)plicit -o:in* operation that occurs in the assi*n)ent o3 p to -o: causes the value o3 p to -e copied. Had Soint -een declared a class instead5 the value 2N would -e output -ecause p and -o: would re3erence the sa)e instance. QG IsnCt it cool to have collections which can ta.e 0oth ints as well as strin-s? 6C Ees5 -ut note that i3 1ou store a value t1pe in a collection5 then in order to update the value 1ou have to un-o:5 update the value5 re-o: the updated value and then replace the o-.ect in the collection. This has two pro-le)sC Ser3or)ance. 6s this article shows5 1ou can al)ost dou-le per3or)ance -1 usin* wrapper classes 7see IntHolderClass9 instead o3 auto-o:in*. Moss o3 identit1. The updated o-.ect is not the sa)e as the old o-.ect. This can -rea+ code that depends on the identit1 o3 the o-.ect. 6*ain5 this pro-le) can also -e solved -1 usin* a wrapper class. QG What can we conclude 6ro, all this? 6C The <T1pe S1ste) Fni3ication< in C# is hal3/-a+ed and 3ull o3 pit3alls. Broperties QG What do properties 0u4 4ou? 6C The )ost o3t >uoted -ene3it is as 3ollowsC Eou can start with pu-lic )e)-er varia-les5 and at a later sta*e in 1our develop)ent process i3 1ou decide that 1ou need to do so)e processin* whenever the varia-le is read or written 1ou can do so -1 chan*in* the pu-lic )e)-er varia-le into a propert1. QG Wh4 doesnCt that wor. in practice? 6C In realit15 i3 1ou chan*e a pu-lic )e)-er varia-le to a propert1 1ou have no *uarantee that 1ou will not -rea+ client code. Gor e:a)ple5 this code will not co)pile i3 1ou chan*e Su) to a propert1C pu-lic void Test79 I Goo7re3 -ar.Su)9J K QG #o properties have an4 other 0ene6its? 6C Sroperties have a cleaner s1nta: -ecause 1ou don<t have to suppl1 the parenthesis. QG #oes o,ittin- the parenthesis ,a.e the s4nta< cleaner? 6C Ees5 i3 DcleanerD to 1ou is 3ewer characters on the screen. No5 i3 DcleanerD to 1ou is so)ethin* that appeals to the sense o3 lo*ic and reason. It should -e noted that Sascal did not re>uire 1ou to suppl1 the parenthesis when 1ou call a 3unction5 i3 that 3unction ta+es no para)eters. C re>uires 1ou to suppl1 the parenthesis even i3 the 3unction ta+es no para)eters. 8an1 people thin+ C is cleaner in this re*ard -ecause it )a+es lo*ical sense to re>uire the parenthesis. QG What case convention should I 6ollow when na,in- properties? 6C Srivate )e)-er na)es should -e*in with a lowercase letter while other )e)-er na)es should -e*in with an uppercase letter. It should -e noted that while this is a *ood convention to 3ollow while riting code5 it is onl1 a convention5 and is not en3orced -1 the co)piler. Hence it is not a *ood idea to rel1 on this convention while reading code.

Sa)ple code included with the .NET S ! does not alwa1s adhere to this convention 7see portal e:a)ple9. 6lso5 so)e tools 7such as :sd9 *enerate code which has wron* case properties. QG How can usin- properties cause ,e to ,iss ine66iciencies? 6C ?uic+5 can 1ou spot the ine33icienc1 in this code? pu-lic void Calc7intYZ 3oo9 I Su) @ NJ 3or 7int i @ NJ i W 3oo.Men*thJ i''9 Su) '@ 3ooYiZJ K What 1ou )a1 have )issed is that ever1 '@ involves two 3unction calls. The onl1 indication o3 this 3act is the uppercase <S< in Su) 7which -1 the wa1 is a convention that is 3re>uentl1 not 3ollowed95 so the ine33icienc1 is not eas1 to spot. Now consider this variation o3 the sa)e codeC pu-lic void Calc7intYZ 3oo9 I Su) @ NJ 3or 7int i @ NJ i W 3oo.Men*thJ i''9 SetSu)7RetSu)79 ' 3ooYiZ9J K Now the ine33icienc1 is *larin*l1 o-vious5 so 1ou )a1 rewrite it li+e thisC pu-lic void Calc7intYZ 3oo9 I int te)p @ NJ 3or 7int i @ NJ i W 3oo.Men*thJ i''9 te)p '@ 3ooYiZJ SetSu)7te)p9J K I3 *ettin* or settin* su) is not cheap 7now or in the 3uture95 the resultin* i)prove)ent in per3or)ance )a1 -e nothin* to sneeLe at. Code that uses properties *ives 1ou no i))ediate 3eed-ac+ on the co)pactness and e33icienc1 o3 the code. QG Are there an4 other pit6alls I should 0e aware o6? 6C ?uic+5 can 1ou spot the data-ase access in this code? pu-lic int Calculate=aise7int percent9 I return 7salar1 ' -onus ' Co))ission9 O percent B 1NNJ K I3 1ou )issed the uppercase <C< in Co))ission 1ou )issed the data-ase access. 7 ata-ase access in a propert1 *etter is not unco))on.9 2e sure to choose a *ood 3ont when readin* code [ one in which uppercase C loo+s ver1 di33erent 3ro) lowercase c. 2etter still5 rewrite 1our code li+e this5 so 1ou can clearl1 see the 3unction callC pu-lic int Calculate=aise7int percent9 I return 7salar1 ' -onus ' RetCo))ission799 O percent B 1NNJ K QG What can we conclude 6ro, all this? 6C Sroperties -u1 ver1 little when wei*hed a*ainst the pro-le)s its use is li+el1 to cause.

"tructs
QG What do structs 0u4 4ou? 6C Structs can so)eti)es i)prove per3or)ance. QG How are structs di66erent 6ro, classes? 6C Structs are stored on the stac+ as opposed to the heap. Structs are value t1pes rather than re3erence t1pes. QG How do structs i,pair code reada0ilit4 and ,aintaina0ilit4? 6C What will this code snippet print? Soint a @ new Soint71N5 1N9J Soint - @ aJ

a.: @ 1NNJ Console.WriteMine7-.:9J The answer depends on whether Soint is a class or a struct. I3 Soint is a class then the answer is 1NN. I3 Soint is a struct then the answer is 1N. Even thou*h structs and classes wor+ di33erentl15 the1 loo+ the sa)e. This )a+es the code hard to 3ollow. C# violates a principle o3 pro*ra))in* lan*ua*e desi*n5 +nown as the principle of uniformity! QG What is the principle o6 uni6or,it4 and wh4 should I care? 6C In The Ss1cholo*1 o3 Co)puter Sro*ra))in*5 Wein-er* e:plains that uni3or)it1 is a ps1cholo*ical principle which sa1s that usersBpro*ra))ers e:pect that thin*s that loo+ si)ilar should do si)ilar thin*s5 and conversel1 that thin*s that loo+ di33erent should do di33erent thin*s. I3 a pro*ra))in* lan*ua*e violates this principle then it increases co)ple:it15 and code written in that lan*ua*e -eco)es harder to understand and )aintain. 2ecause o3 C#<s violation o3 the principle o3 uni3or)it15 the pro*ra))er has the e:tra -urden o3 trac+in* whether so)ethin* is a struct or a class. QG IsnCt that a pro0le, in C** too? 6C No. In C''5 structs and classes wor+ the sa)e. The onl1 di33erence is the de3ault visi-ilit1 3or 3ields and )ethods. 6lso5 in C'' the s1nta: 3or usin* a classBstruct re3erence is ver1 di33erent 3ro) the s1nta: 3or usin* a classBstruct value. QG Wh4 should I trac. whether so,ethin- is a struct or a class? 6C It<s a hu*e se)antic di33erence. 6 class varia-le holds a re3erence to to a heap/allocated o-.ect5 while a struct varia-le directl1 holds the value o3 the o-.ect. In C''5 this is usuall1 visi-le in the declaration o3 the varia-le 7Goo 3 3or a value t1pe5 Goo OpG 3or a pointer9 and in its use 73.: 3or a value t1pe5 3/X: 3or a re3erence t1pe9. In C#5 -oth use the sa)e s1nta: 3or declaration and use5 and there isn<t even an1 su**ested na)e standard to )ar+ the di33erence. In .ust a-out ever1 case5 it<s i)portant to +now i3 1ou are dealin* with value t1pes or re3erence t1pes [ re3erence t1pes has the concept o3 identit1 and e>ualit15 while value t1pes onl1 has e>ualit1. 6llocatin* an arra1 o3 value t1pes *ives 1ou a nu)-er o3 o-.ects with de3ault values while an arra1 o3 re3erence t1pes *ives 1ou a arra1 o3 null re3erences. 6ddin* a struct t1pe to a list )eans it<s silentl1 -o:ed and re)ovin* it un-o:es it at a si*ni3icantl1 hi*her cost than i3 a re3erence t1pes were added re)oved. Etc5 etc. YcreditC 8ats 4lssonZ QG What can we conclude 6ro, all this? 6C Structs -u1 ver1 little when wei*hed a*ainst the )aintenance headaches its use is li+el1 to cause ?ser2de6ined i,plicit conversions QG What do user2de6ined i,plicit conversions 0u4 4ou? 6C Fser/de3ined i)plicit conversions allow 1our class to de3ine conversions 3ro) other classes 7or -asic t1pes95 without re>uirin* a cast5 or re>uirin* 1ou to call a conversion 3unction. Gor e:a)ple5 i3 1ou have a class called Co)ple: and 1ou have de3ined an i)plicit conversion 3ro) 3loat then 1ou can use it li+e thisC Co)ple: c1 @ 12.3$&GJ Without user/de3ined i)plicit conversions5 1ou would have to use one o3 the 3ollowin* less/convenient state)entsC Co)ple: c2 @ 7Co)ple:912.3$&GJ Co)ple: c3 @ Co)ple:.#alue43712.3$&G9J QG IsnCt this what wrea.ed havoc in &5'? 6C Ees. #2$ did not have the a-ilit1 3or users to de3ine their own i)plicit conversions. Instead5 it ca)e with a lar*e set o3 -uilt/in5 DconvenientD conversions. This supposed DconvenienceD caused so )an1 pro-le)s that #2 pro*ra))ers nic+na)ed this 3eature DEvil T1pe CoercionD. Eou can read )ore a-out it hereC httpCBBwww.dev:.co)Bpre)ierB)*LnarchBv-p.B1;;&B11nov;&Bpro*tech.pd3 QG #onCt ,an4 pro-ra,,in- lan-ua-es have 0uilt2in i,plicit conversions? 6C Ees5 )an1 pro*ra))in* lan*ua*es includin* C do so)e i)plicit t1pe conversions5 such as 3ro) int to lon*. 2ut such -uilt/in i)plicit conversions are usuall1 li)ited5 well understood and ver1 strict. QG How does i,plicit t4pe conversion reduce so6tware relia0ilit4? 6C I)plicit t1pe conversions decrease t1pe error detection a-ilit1 o3 the co)piler. Gewer o3 1our pro*ra))in* errors will -e cau*ht -1 the co)piler5 which leads to less relia-le so3tware. Gor e:a)ple5 i3 1ou erroneousl1 assi*ned an inte*er to a varia-le o3 t1pe Goo5 the co)piler will catch the error. However i3 1ou have de3ined an i)plicit conversion 3ro) inte*er to Goo5 then the co)piler will assu)e 1ou )eant to

convert the inte*er to t1pe Goo and then store the value5 so 1our error will not -e cau*ht. I3 this is reall1 what 1ou )eant5 then it is -etter to )a+e it e:plicit -1 callin* a conversion 3unction li+e thisC Goo 3 @ Goo.#alue4371239J QG Are i,plicit t4pe conversions 7$ when there is no loss o6 precision? 6C The issues with i)plicit t1pe conversion have nothin* to do with whether there is loss o3 precision or not. See a-ove. QG What can we conclude 6ro, all this? 6C The evils o3 i)plicit t1pe conversion outwei*h the -ene3its.

?nchec.ed <ceptions
QG What is the pro0le, with chec.ed e<ceptions? 6C With chec+ed e:ceptions5 1ou have to either catch e:ceptions thrown -1 the )ethods 1ou call5 or )ention the e:ception in the throws clause o3 1our )ethod. 2ecause o3 the tediu) o3 )entionin* each e:ception in the throws clause5 so)e pro*ra))ers )a1 ta+e the shortcut o3 s allo ing e:ceptions. QG What is ,eant 04 swallowing an e<ception? 6C Eou should onl1 catch e:ceptions that 1ou are prepared to handle. 4therwise5 i3 1ou catch an e:ception and i*nore it 7or lo* it95 1ou are Dswallowin*D the e:ception. 6s a result5 code hi*her up the call chain that )a1 -e prepared to handle the e:ception will never see the e:ception. QG #oes C# solve the pro0le, o6 swallowed e<ceptions? 6C No. In 3act5 C# re>uires itP 8ethods in C# cannot speci31 the e:ceptions it )a1 throw. The docu)entation o3 the )ethod 1ou wish to call )a1 )ention the list o3 e:ceptions 1ou can e:pect. 2ut this list o3 e:ceptions is not en3orced -1 the co)piler5 and is not *uaranteed to -e e:haustive. 6lso5 since the list o3 e:ceptions is not part o3 the contract5 the )ethod can -e revised at an1 ti)e and a new e:ception that 1ou didn<t +now a-out can -e thrown. Since there is no previousl1 a*reed upon list o3 e:ceptions5 1ou are le3t with no option -ut to catch all e:ceptions i3 1ou want to recover 3ro) an e:ception 7such as -1 o33erin* to connect to an alternate data-ase i3 the pri)ar1 data-ase is down5 etc.9 4-viousl15 i3 1ou catch all e:ceptions5 1ou will end up swallowin* so)e o3 the). QG What other pro0le,s e<ist with C# e<ceptions? 6C 43ten in C#5 i3 1ou have )ultiple i)ple)entations o3 an inter3ace5 each o3 those i)ple)entations )a1 have their own dis.oint e:ception hierarchies. 7i.e.5 the e:ception hierarchies have no co))on classes other than S1ste).E:ception.9 E:a)pleC 6 4.NET. This )a+es it i)possi-le 3or *eneric code 7i.e.5 code that can use an1 o3 those i)ple)entations9 to recover 3ro) e:ceptions *enericall1 without catchin* all e:ceptions 7i.e.5 S1ste).E:ception.9 6*ain5 i3 1ou catch all e:ceptions 1ou will end up swallowin* so)e o3 the). QG What can we conclude 6ro, all this? 6C Fnchec+ed e:ceptions results in shorter -ut less ro-ust code. How do I write a ,ethod that accepts a varia0le nu,0er o6 para,eters? QG How do I write a ,ethod that accepts a varia0le nu,0er o6 para,eters? 6C Man*ua*es li+e C and C'' have alwa1s o33ered so)e )eans 3or usin* and creatin* 3unctions capa-le to accept a varia-le nu)-er o3 para)eters. The )ost widel1 +nown e:a)ple o3 a 3unction which ta+es a varia-le nu)-er o3 para)eters is print379C int print37char O3or)at5 \9J BB the ellipsis )eans Dvaria-le nu)-er o3 para)sD Fsin* this 3unction is prett1 eas1C print37DHello5 worldUnD9J print37DThe su) o3 ]d and ]d is ]dUnD5 a5 -5 a'-9J However5 the creation o3 such 3unctions in these lan*ua*es rela1s on a set o3 prede3ined )acros and is not particularl1 ele*ant or intuitive. C# o33ers an ele*ant solution to this pro-le) throu*h para)eter arra1s. 6 para)eter arra1 is a sin*le/di)ensional arra1 included as the last para)eter in the para)eter list o3 a )ethodC pu-lic strin* Concat7strin* separator5 para)s strin*YZ strin*s9 I strin* result @ DDJ 3or 7int i @ NJ i W strin*s.Men*thJ i''9

I i3 7i X N9 result '@ separatorJ result '@ strin*sYiZJ K return resultJ K Such a 3unction can -e called in two di33erent wa1sC a9 Sassin* the 3unction an arra1 instance ar*u)entC strin*YZ na)es @ I D6ndersD5 DEricD5 DScottD5 D uncanD KJ :essa-e5o<.Show7Concat7D'D5 na)es9 ' D @ *reat tea)D9J -9 Fsin* Lero or )ore t1pe/co)pati-le ar*u)ents 3or the para)eter arra1C :essa-e5o<.Show7Concat7D'D5 D6ndersD5 DEricD5 DScottD5 D uncanD9 ' D @ *reat tea)D9J In this case5 the invocation will create an arra1 3ro) the supplied ar*u)ents and use it as the actual ar*u)ent. Than+s to the uni3ied .NET t1pe s1ste)5 o-.ectYZ can -e used as ^co))on deno)inator_ 3or ar*u)ents o3 di33erent t1pesC pu-lic int Su)TheInte*ers7para)s o-.ectYZ list9 I BB su) all the inte*ers included in list int su) @ NJ 3oreach 7o-.ect o in list9 i3 7o.RetT1pe79 @@ t1peo37int99 su) '@ 7int9 oJ return su)J K Wh4 doesnCt C# support static ,ethod varia0les? ?C In C''5 it<s possi-le to write a static )ethod varia-le5 and have a varia-le that can onl1 -e accessed 3ro) inside the )ethod. C# doesn<t provide this 3eature. Wh1? 6C There are two reasons C# doesn<t have this 3eature. Girst5 it is possi-le to *et nearl1 the sa)e e33ect -1 havin* a class/level static5 and addin* )ethod statics would re>uire increased co)ple:it1. Second5 )ethod level statics are so)ewhat notorious 3or causin* pro-le)s when code is called repeatedl1 or 3ro) )ultiple threads5 and since the de3initions are in the )ethods5 it<s harder to 3ind the de3initions. Wh4 ,ust attri0ute properties 0e readHwrite in C#? ?C Wh1 )ust attri-ute properties -e readBwrite in C#? In the C# lan*ua*e5 when 1ou de3ine an attri-ute class5 1ou can de3ine -oth constructor ar*u)ents and properties. Gor e:a)pleC class 816ttri-uteC 6ttri-ute I strin* na)eJ int nu)-erJ pu-lic 816ttri-ute7strin* na)e9 I this.na)e @ na)eJK pu-lic int Nu)-er I *et Ireturn nu)-erJK set Inu)-er @ valueJ K K pu-lic strin* Na)e I *et Ireturn na)eJK K K

Y817DGredD5 Nu)-er@&9Z class Gred I K When 1ou do this5 C# re>uires that properties 7such as Nu)-er in this e:a)ple9 -e readBwrite properties5 thou*h lan*ua*es such as #2.NET onl1 re>uire the) to -e reada-le. Wh1? 6C Fsin* an attri-ute in C# is lo*icall1 e>uivalent to callin* the constructor o3 the attri-ute class5 and then e:ecutin* the ^propert1 @ value_ part 3or each na)ed propert1. =e>uirin* a writea-le propert1 is consistent with this view. Wh4 do I -et the error I701ect re6erence not set to an instance o6 an o01ectI? The code is tr1in* to access a )e)-er o3 a re3erence t1pe varia-le that is set to null. Riven the 3ollowin* class5 let<s e:a)ine so)e code that could -e in the Main )ethodC usin* S1ste)J class Goo I static void 8ain79 I BB 3oo has not -een instantiated Goo 3oo @ nullJ BB i)ple)entation to -e discussed... K pu-lic int 2arSropert1 I *et I return NJ K K pu-lic strin* 2ar8ethod79 I return nullJ K K Moo+in* at Main5 notice that foo is set to null. Eour situation will not -e this e:plicit -ecause the varia-le 1ou are tr1in* to use could -e a class 3ield5 para)eter5 or local varia-le that was once instantiated -ut was su-se>entl1 set to null. Riven that foo is null5 the 3ollowin* code will throw a NullReferenceExceptionC tr1 I BB 3oo is null5 and 1ou can<t call BB 2arSropert1 on null. int result2ad @ 3oo.2arSropert1J K catch 7Null=e3erenceE:ception nre9 I Console.WriteMine7 DUnCan<t read 2arSropert15 3oo is null.UnD ' nre.8essa*e9J K Since foo is null5 1ou can not use its )e)-ers. This was a propert15 -ut the 3ollowin* code de)onstrates callin* a )ethodC tr1 I BB 3oo is null5 and 1ou can<t call BB 2ar8ethod on null. 3oo.2ar8ethod79J K catch 7Null=e3erenceE:ception nre9 I Console.WriteMine7 DUnCan<t call 2ar8ethod795 3oo is null.UnD '

nre.8essa*e9J K The code a-ove throws a NullReferenceException -ecause foo is still null. It doesn<t )atter that the 3irst call was a propert1 and the second is a )ethod / the1 are -oth )e)-ers o3 the t1pe. Now5 to 3i: this pro-le)5 1ou )ust ensure that foo is instantiated -e3ore it is used. The 3ollowin* code instantiates foo and the previous pro-le)s are solvedC BB now we have an instance 3oo @ new Goo79J BB wor+s 3ine int resultRood @ 3oo.2arSropert1J Since foo now re3ers to a valid instance5 the code can call an1 o3 its )e)-ers. This was easier than )an1 null re3erence pro-le)s. So)eti)es 1ou have )ultiple levels o3 indirection5 leavin* 1ou with a scenario 1ou didn<t e:pect. 6ssu)in* that foo now re3erences an o-.ect5 there is still a pro-le) with the 3ollowin* codeC tr1 I BB still -rea+s -ecause 2ar8ethod79 returned BB null and 1ou can<t call Tri)79 on null. 3oo.2ar8ethod79.Tri)79J K catch 7Null=e3erenceE:ception nre9 I Console.WriteMine7 DUnCan<t call Tri)795 2ar8ethod79 returns null.UnD ' nre.8essa*e9J K The pro-le) occurs in the code a-ove -ecause "arMethod returned null5 which )eans that the code is tr1in* to call the #rim )ethod on a strin* re3erence that is set to null. The proper wa1 to 3i: this pro-le) is to de-u* "arMethod to 3ind out wh1 it returned null. That assu)es "arMethod is not supposed to return null. I35 in 1our application5 it )ade sense 3or "arMethod to so)eti)es return null5 then 1ou would have to chec+ the return value o3 "arMethod -e3ore 1ou called an1 )e)-ers o3 the return value. Wh4 did I receive the errorG I!he t4pe or na,espace CJna,espace na,eKC does not e<ist in the class or na,espace CJparent na,espaceKC 9are 4ou ,issin- an asse,0l4 re6erence?;I Eou need to add a re3erence in 1our pro.ect to an asse)-l1 where that na)espace is de3ined. I3 1ou are usin* #S.NETC 1. =i*ht clic+ on the =e3erences 3older on 1our pro.ect. 2. Select 6dd =e3erence. 3. Select the .NET ta- 7or select the 2rowse -utton i3 it is not a .NET Gra)ewor+ asse)-l19. $. ou-le/clic+ the asse)-l1 containin* the na)espace in the error )essa*e. &. Sress the 4! -utton. I3 1ou are usin* the co))and line5 use the BrC or Bre3erenceC option. Gor E:a)pleC csc!exe $reference%&ystem!'ra ing!dll MyFont'isplayApp!cs When 1ou reco)pile5 this error will no lon*er appear. Eou can 3ind the asse)-l15 where a na)espace is de3ined5 in the docu)entation. Identi31 one o3 the t1pes in the na)espace that 1ou want to use. Ever1 t1pe in the .NET Gra)ewor+ has an D6-outD pa*e that provides an overview5 -asic in3or)ation a-out the t1pe5 and e:a)ple code i3 1ou<re luc+1. 6t the -otto) o3 the 4verview5 3or all t1pes5 there is a D=e>uire)entsD section. This has a Na)espace )e)-er that tells what na)espace the t1pe -elon*s to. It also tells what asse)-l1 t1pe -elon*s to5 which is the asse)-l1 1ou need to re3erence in 1our application. Gor e:a)ple5 i3 I had an application that was usin* Gont t1pes5 I would loo+ up Gont in the .NET Gra)ewor+ docu)entation5 usin* the Inde: ta-5 and o-serve that the Gont t1pe is in the S1ste). rawin* na)espace and its asse)-l1 is S1ste). rawin*.dll. 6nother >uestion related to this is DI3 I<ve alread1 declared a using state)ent5 wh1 do I have to add the re3erence to the pro.ect?D The reason derives 3ro) the 3act that the using state)ent and asse)-l1 re3erences have two di33erent purposes. =ecall that the purpose o3 na)espaces is to disa)-i*uate t1pe re3erences and provide lo*ical or*aniLation o3 t1pes. When speci31in* a na)espace in a using state)ent5 1ou are tellin* C# that 1ou want to use the t1pes in that na)espace in an un>uali3ied )anner. The +e1 point is that na)espaces are Dlo*icalD entities that could e:ist in one or )ore asse)-lies. 4n the other hand5 asse)-lies are Dph1sicalD entities that contain )ultiple t1pes. 6sse)-lies are )an1 thin*s5 -ut 3or the purposes o3 this discussion5 an asse)-l1 is the unit o3 deplo1)ent in .NET. So5 the reason 1ou need a re3erence to the asse)-l1 that the t1pe is located in is so that C# can 3ind the ph1sical location o3 that t1pe5 which is not possi-le with the lo*ical na)espace provided -1 a usin* state)ent.

How can I su0scri0e to events e<posed 04 a re,oted o01ect? ele*ates re>uire in3or)ation a-out the t1pe that the )ethod is associated with in order to )a+e a call. In a sin*le app/do)ain5 this isn<t a pro-le)5 -ecause the server 7the o-.ect 3irin* the events9 has access to the t1pe in3or)ation 3or the client 7which has the event handlers9 -1 wa1 o3 the dele*ate. However5 durin* re)otin*5 the server )ost li+el1 does not have an1 in3or)ation a-out the client. I3 1ou want events 3ro) the server to 3ire in the client app do)ain5 then the client )ust derive 3ro) 8arshal21=e34-.ect. This is re>uired so that the server will call -ac+ into the client5 as opposed to a cop1 o3 the client o-.ect that is passed to the server. 6 si)ple wa1 to do this is to place a cop1 o3 the client asse)-l1 in the sa)e director1 where the server director1 is. While this will wor+5 it is not an ele*ant solution5 as it e:poses the client t1pe unecessaril1. 6 )ore ele*ant solution 7al-iet )ore co)ple:95 is to have a sin*le asse)-l1 that is re3erenced -1 -oth the client and the server. This asse)-l1 will have a shi) class which will e:pose )ethods that share the si*nature o3 the events that 1ou want to shi). The asse)-l1 will also provide an inter3ace with )ethods that share the si*nature o3 the events as well. The shi) will then ta+e a re3erence to the inter3ace5 and the )ethods will a**re*ate the call to the inter3ace that was passed in 7I reco))end the constructor as the place to pass the inter3ace i)ple)entation9. Ginall15 the client o-.ect will i)ple)ent the inter3ace. When su-scri-in* to the events that the server e:poses5 attach the dele*ates to the )ethods on the shi)5 which will -e passed 1our client o-.ect<s i)ple)entation. It is also i)portant to note that in order 3or this to wor+5 the T1peGilterMevel propert1 on the sin+ provider 3or the server needs to -e set to T1peGilterMevel.Gull. Wh4 does ,4 Windows For, pro1ect not use visual st4les in AB even when I call Application. na0le&isual"t4les? I3 1ou set a propert1 on a Windows Gor)s control which 3orces the creation o3 the control 7e.*. the SelectedInde: propert1 on the Co)-o2o: class95 the control 7and perhaps the rest o3 the 3or)9 will not render with visual st1les ena-led. The resolution is to place the code that sets these properties in an event handler 3or the Moad event on the 3or)Bcontrol. Wh4 arenCt re6erence t4pes pol4,orphic? ?C Wh1 aren<t re3erence t1pes pol1)orphic? 6C Consider the 3ollowin* codeC usin* S1ste)J class o* I pu-lic strin* Na)eJ K class Test I pu-lic static void Swap7re3 o-.ect a5 re3 o-.ect -9 I o-.ect te)pJ te)p @ aJ a @ -J - @ te)pJ K pu-lic static void 8ain79 I o* d1 @ new o*79J d1.Na)e @ D3idoDJ o* d2 @ new o*79J d2.Na)e @ Dre:DJ Swap7re3 d15 re3 d29J K K The co)piler will report an error on the call to the Swap79 3unction. Wh1? Consider i3 the swap 3unction was li+e thisC pu-lic static void Swap7re3 o-.ect a5 re3 o-.ect -9 I a @ &J - @ ^Hello^J K I3 the co)piler allowed this code5 it would )ean assi*nin* a -o:ed int to a o* o-.ect5 which is clearl1 not t1pe sa3e. When should I use EE and when should I use Duals?

The E>uals )ethod is .ust a virtual one de3ined in S1ste).4-.ect5 and overridden -1 whichever classes choose to do so. The @@ operator is an operator which can -e overloaded -1 classes5 -ut which usuall1 has identit1 -ehaviour. Gor re3erence t1pes where @@ has not -een overloaded5 it co)pares whether two re3erences re3er to the sa)e o-.ect / which is e:actl1 what the i)ple)entation o3 E>uals does in S1ste).4-.ect. #alue t1pes do not provide an overload 3or @@ -1 de3ault. However5 )ost o3 the value t1pes provided -1 the 3ra)ewor+ provide their own overload. The de3ault i)ple)entation o3 E>uals 3or a value t1pe is provided -1 #alueT1pe5 and uses re3lection to )a+e the co)parison5 which )a+es it si*ni3icantl1 slower than a t1pe/speci3ic i)ple)entation nor)all1 would -e. This i)ple)entation also calls E>uals on pairs o3 re3erences within the two values -ein* co)pared. However5 the )ain di33erence -etween the two t1pes o3 co)parison in nor)al use 7where 1ou<re unli+el1 to -e de3inin* 1our own value t1pes ver1 o3ten9 is pol1)orphis). 4perators are overloaded5 not overridden5 which )eans that unless the co)piler +nows to call the )ore speci3ic version5 it<ll .ust call the identit1 version. To illustrate that5 here<s an e:a)pleC usin* S1ste)J pu-lic class Test I static void 8ain79 I BB Create two e>ual -ut distinct strin*s strin* a @ new strin*7new charYZ I<h<5 <e<5 <l<5 <l<5 <o<K9J strin* - @ new strin*7new charYZ I<h<5 <e<5 <l<5 <l<5 <o<K9J Console.WriteMine 7a@@-9J Console.WriteMine 7a.E>uals7-99J BB Now let<s see what happens with the sa)e tests -ut BB with varia-les o3 t1pe o-.ect o-.ect c @ aJ o-.ect d @ -J Console.WriteMine 7c@@d9J Console.WriteMine 7c.E>uals7d99J K K The results areC True True Galse True The third line is Galse -ecause the co)piler can onl1 call the non/overloaded version o3 @@ as it doesn<t +now that the contents o3 c and d are -oth strin* re3erences. 6s the1 are re3erences to di33erent strin*s5 the identit1 operator returns 3alse. So5 when should 1ou use which operator? 81 rule o3 thu)- is that 3or al)ost all re3erence t1pes5 use E>uals when 1ou want to test e>ualit1 rather than re3erence identit1. The e:ception is 3or strin*s / co)parin* strin*s with @@ does )a+e thin*s an aw3ul lot si)pler and )ore reada-le but 1ou need to re)e)-er that -oth sides o3 the operator )ust -e e:pressions o3 t1pe strin* in order to *et the co)parison to wor+ properl1. Gor value t1pes5 I<d nor)all1 use @@ 3or easier/to/read code. Thin*s would *et tric+1 i3 a value t1pe provided an overload 3or @@ which acted di33erentl1 to E>uals5 -ut I<d consider such a t1pe ver1 -adl1 desi*ned to start with. "hould I assi-n null to ,4 local varia0les? ?C Should I assi*n null to )1 local varia-les a3ter I use the)? Gor e:a)pleC strin* s @ ...J Console.WriteMine7s9J s @ nullJ 6C There is rarel1 a need to do this 3or local varia-les in C# The li3eti)e o3 varia-les is trac+ed -1 the %IT / it anal1Les how varia-les are used in a routine5 and it +nows e:actl1 when the varia-le is no lon*er needed5 and a3ter that point5 the value is availa-le to -e collected. Interestin*l15 i3 1ou assi*n it to null5 1ou are actuall1 e:tendin* the li3eti)e o3 the varia-le sli*htl15 so it could cause it to -e *ar-a*e collected later 7thou*h realisticall15 there<s unli+el1 to -e an1 real di33erence here.

This is true 3or the vast )a.orit1 o3 )ethods. I3 1ou have a )ethod where 1our code lives 3or a while / a loop on a separate thread5 3or e:a)ple / then it )a1 -e worthwhile to see i3 there are an1 unnecessar1 values livin* in varia-les while 1ou wait. Wh4 do I need a null test 0e6ore I invo.e a dele-ate? ?C Wh1 do I need a null test -e3ore I invo+e a dele*ate? 6C I3 1ou have an event in a class5 1ou need to add a null test -e3ore 1ou call the dele*ate. T1picall15 1ou would writeC i3 7Clic+ P@ null9 Clic+7ar*15 ar*29J There is actuall1 a possi-le race condition here / the event can -e cleared -etween the 3irst and second line. Eou would actuall1 want to writeC Clic+Handler handler @ Clic+J i3 7handler P@ null9 handler7ar*15 ar*29J usuall1. Eou )i*ht want to do so)e other sort o3 s1nchroniLation in other scenarios. So -ac+ to the )ain >uestion. Wh1 is the null test re>uired? We can<t chan*e the e:istin* -ehavior o3 callin* throu*h a dele*ate as so)e apps )a1 depend on it5 so it would have to -e an addition to the lan*ua*e. We have tal+ed a-out addin* an Invo+e79 +e1word to the lan*ua*e to )a+e this easier5 -ut a3ter a 3air -it o3 discussion5 we decided that we couldn<t do the ri*ht thin* all the ti)e5 so we elected not to do an1thin*. Wh4 doesnCt C# warn a0out unused ,ethods? ?C Wh1 doesn<t C# warn on unused )ethods? 6C This is so)ethin* that the C# co)piler could do5 su-.ect to a 3ew caveatsC #irtual 3unctions would have to -e e:cluded Inter3ace i)ple)entations wouldn have to -e e:cluded Eou would *et 3alse positives i3 1ou used re3lection Eou )i*ht *et 3alse positives i3 1ou used dele*ates 7the co)piler )i*ht -e a-le to -e s)art here9 4ther than that5 it would -e possi-le 3or us to do this5 and we )a1 do it in a 3uture version. How can I update ,4 user inter6ace 6ro, a thread that did not create it? When per3or)in* an1 action on a control which re>uires the updatin* o3 a user inter3ace ele)ent 7e.*. settin* the Te:t propert1 on al)ost an1 class derived 3ro) Control5 updatin* the data source -ehind a ataRrid95 these operations 8FST ta+e place on the thread that created the FI ele)ent. In order to do this5 the Control class provides the Invo+e )ethod5 which will ta+e a dele*ate and e:ecute it on the thread that the FI ele)ent was created on. In order to use this5 one )ust declare a 3unction that per3or)s the FI operation. Gor e:a)ple5 sa1 a 3or) has a Te:t2o: on it na)ed )HTe:t2o:. To update the te:t 3ro) another thread5 create a )ethod that will update the Te:t propert1 on the Te:t2o:C BB The declaration o3 the te:t-o:. private Te:t2o: )HTe:t2o:J BB Fpdates the te:t-o: te:t. private void FpdateTe:t7strin* te:t9 I BB Set the te:t-o: te:t. )HTe:t2o:.Te:t @ te:tJ K Now5 create a dele*ate that has the sa)e si*nature as the )ethod that was previousl1 de3inedC pu-lic dele*ate void FpdateTe:tCall-ac+7strin* te:t9J In 1our thread5 1ou can call the Invo+e )ethod on )HTe:t2o:5 passin* the dele*ate to call5 as well as the para)eters. )HTe:t2o:.Invo+e7new FpdateTe:tCall-ac+7this.FpdateTe:t95 new o-.ectYZI_Te:t *enerated on non/FI thread._K9J NoteC o not create a )ethod that )atches the EventHandler dele*ate si*nature and pass that. The i)ple)entation o3 Invo+e on the Control class will not ta+e into account the para)eters passed to Invo+e i3 the t1pe o3 the dele*ate is EventHandler. It will pass the control that Invo+e was called on 3or the sender para)eter as well as the value returned -1 Event6r*s.E)pt1 3or the e para)eter. Wh4 canCt I have static and instance ,ethods with the sa,e na,e? ?C Wh1 can<t I have static and instance )ethods with the sa)e na)e? class Test I

static void Srocess79J void Srocess79J void 6)-i*uousCaller79 I Srocess79J K K there<s no a)-i*uit15 since the 3irst can onl1 -e called throu*h the t1pe na)e5 and the second can onl1 -e called throu*h an instance. 6C It is true that there would -e no a)-i*uit1 -etween the two 3unctions as 3ar as a co)piler is concerned. There would5 however5 -e a considera-le potential 3or con3usion on the part o3 the user. It would -e tou*h to 3ind the ri*ht )ethod in docu)entation5 and once 1ou did5 hard to -e sure that 1ou are callin* the ri*ht version 7ie 1ou could accidentall1 call the static version when 1ou wanted the instance version9. We there3ore prohi-it this case. Where can I 6ind sa,ple C# code 6or si,ple threadin-? =e3er to the S1ste).Threadin* na)espace on 8S N 3or 3ull details. 8eanwhile here is a >uic+ taste. usin* S1ste)J usin* S1ste).Threadin*J class ThreadTest I pu-lic void =un)e79 I Console.WriteMine7D=un)e CalledD9J Thread.Sleep71NNNN9J K pu-lic static void 8ain7Strin*YZ ar*s9 I ThreadTest - @ new ThreadTest79J Thread t @ new Thread7new ThreadStart7-.=un)e99J t.Start79J Console.WriteMine7DThread <t< started.D9J Console.WriteMine7DThere is no tellin* when D ' D<=un)e< will -e invo+ed. D9J t.%oin79J Console.WriteMine7DThread <t< has ended.D9J K K How do C# -enerics co,pare to C** te,plates? ?C How do C# *enerics co)pare to C'' te)plates? 6C This is reall1 a 3airl1 co)ple: topic. 6nders has touched on it in an interview. I should state at the outset that the *oals o3 *enerics are not the sa)e as the *oals o3 te)plates. There are so)e thin*s that te)plates do -etter than *enerics5 and vice versa. :odel C'' te)plates use a co)pile/ti)e )odel. When a te)plate is used in a C'' pro*ra)5 the e33ect is as i3 a sophisticated )acro processor had -een used. C# *enerics are not .ust a 3eature o3 the co)piler5 -ut also a 3eature o3 the runti)e. 6 *eneric t1pe such as MistWTX )aintains its *eneric/ness 7*enericit19 a3ter it has -een co)piled. 4r5 to loo+ at it another wa15 the su-stitution that the C'' co)piler does at co)pile ti)e is done at %IT ti)e in the C# *eneric world. rror Chec.inError is -est illustrated throu*h an e:a)ple. Consider a te)plate that has a )ethod li+e thisJ T 6dd7T t15 Tt29 I return t1 ' t2J K

the C'' co)piler will -e a-le to parse this )ethod success3ull1. When this te)plate is actuall1 used5 the Ts will -e replaced with an actual t1pe. I3 a t1pe such as int is used5 the co)piler will -e a-le to create the 6dd )ethod correctl15 as it +nows how to add two ints. I3 a t1pe such as E)plo1ee was used5 the co)piler would issue an error5 as the co)piler would +now that there was no wa1 to add two E)plo1ees. The *enerics world is ver1 di33erent. 2ecause the t1pe that is used with the *eneric is not +nown at co)pile ti)e5 the co)piler needs additional in3or)ation a-out the t1pe that will -e used with a *eneric class. This is done throu*h constraints5 which allow the author to constrain the t1pes that can -e used with a *eneric t1pe. Gor e:a)pleC Class MistWTX where TCICo)para-le )eans that whenever I use a T in )1 i)ple)entation5 I can call the Co)pareTo79 3unction on it. Constraints could theoreticall1 provide nearl1 the sa)e level o3 3le:i-ilit1 that te)plates do5 -ut that would re>uire a ver1 co)ple: constraint s1nta:. Gor the Whid-e1 release5 onl1 certain operations can -e speci3ied throu*h contraints5 which li)its the nu)-er o3 operations 1ou can per3or). Gor e:a)ple5 there is no wa1 to sa1 that a *eneric t1pe )ust have an add operator5 so 1ou can<t write ^a ' -^ in a *eneric class. It is possi-le to wor+ around this at runti)e usin* re3lection5 -ut the i)ple)entation isn<t as clean and there )a1 -e a per3or)ance loss. We )a1 address so)e o3 these issues in 3uture releases >un2ti,e operations Renerics in C# have 3ull run/ti)e support. I3 1ou use re3lection5 1ou will 3ind that 1ou can re3lect over *eneric t1pes5 and create the) at runti)e. There<s no real analo* o3 this in the C'' world. "pace ?se The use o3 space is di33erent -etween C'' and C#. 2ecause C'' te)plates are done at co)pile ti)e5 each use o3 a di33erent t1pe in a te)plate results in a separate chun+ o3 code -ein* created -1 the co)piler. In the C# world5 it<s so)ewhat di33erent. The actual i)ple)entations usin* a speci3ic t1pe are created at runti)e. When the runti)e creates a t1pe li+e MistWintX5 the %IT will see i3 that has alread1 -een created. I3 it has5 it )erel1 users that code. I3 not5 it will ta+e the IM that the co)piler *enerated and do appropriate replace)ents with the actual t1pe. That<s not >uite correct. There is a separate native code path 3or ever1 value t1pe5 -ut since re3erence t1pes are all re3erence/siLed5 the1 can share their i)ple)entation. This )eans that the C# approach should have a s)aller 3ootprint on dis+5 and in )e)or15 so that<s an advanta*e 3or *enerics over C'' te)plates. In 3act5 the C'' lin+er i)ple)ents a 3eature +nown as ^te)plate 3oldin*^5 where the lin+er loo+s 3or native code sections that are identical5 and i3 it 3inds the)5 3olds the) to*ether. So it<s not a clear/cut as it would see) to -e. !e,plate ,etapro-ra,,inC'' te)plates are so)eti)es used 3or a techni>ue +nown as te)plate )etapro*ra))in*. There is no wa1 to do this in C#. Where can I -et a 6ull co,parison 0etween C# and &5.N !? 8icroso3t provides a ver1 3ull lan*ua*e e>uivalents pa*e which co)pares not onl1 C# and #2.NET5 -ut also other lan*ua*es tar*eted at the .NET 3ra)ewor+. It loo+s at the e>uivalent concepts5 +e1words5 t1pes5 operators etc. 6 ver1 valua-le resource when 1ou<re tr1in* to read or write code in a lan*ua*e which isn<t 1our pre3erred one. Is there an eDuivalent o6 :4Class? No5 C# doesn<t have an e>uivalent o3 #2.NET<s 81Class +e1word. I3 1ou want to *uarantee not to call an overridden version o3 a )ethod5 1ou need to )a+e it non/virtual in the 3irst place. What do I use instead o6 addresso6? To create dele*ate instances in C#5 1ou .ust speci31 the dele*ate t1pe5 the )ethod5 and 7i3 1ou want to create a dele*ate tar*ettin* a di33erent instance or t1pe 3ro) the current one9 the tar*et. Gor instance5 each o3 these creates a ThreadStart dele*ateC ThreadStart :1 @ new ThreadStart7So)eInstance8ethod9J ThreadStart :2 @ new ThreadStart76notherT1pe.So)eStatic8ethod9J ThreadStart :3 @ new ThreadStart7so)e#aria-le.So)eInstance8ethod9J How do I -et the ri-ht,ost part o6 a strin-8 as with the &5 >i-ht 6unction? Fse Strin*.Su-strin*. 6ssu)in* that : is a strin* o3 len*th at least n5 to *et the last n characters5 1ou would use :.Su-strin*7:.Men*th/n9. Note that the a-ove assu)es that the strin* is at least n characters lon*. Gor a )ore ro-ust version5 1ou )i*ht use so)ethin* li+eC :.Men*th W n ? :.Su-strin*7:.Men*th/n9 C :. What are the eDuivalents o6 :e and :45ase?

8e in C# is this5 and 812ase in C# is -ase. To access nor)al )e)-ers5 .ust use this.)e)-erNa)e or -ase.)e)-erNa)e. Gor in3or)ation a-out chainin* constructors to*ether5 see )1 article on constructors. WhatCs the eDuivalent o6 Nothin-? Gor re3erence t1pes5 the e>uivalent o3 #2<s Nothin* is C#<s null. Gor value t1pes5 it<s the de3ault value / N5 3alse5 etc. How do I tell C# what .ind o6 literal nu,0er I want? I3 1ou need to tell C# that 1ou want it to treat a literal as a particular t1pe o3 nu)-er5 1ou )a1 do so -1 addin* a nu)-er t1pe su33i: at the end o3 the literal 1ou provide. Gor e:a)pleC 1uJ BB 6n unsi*ned int 1lJ BB 6 si*ned lon* 1ulJ BB 6n unsi*ned lon* 13J BB 6 S1ste).Sin*le 3loatin*/point nu)-erJ 1dJ BB 6 S1ste). ou-le 3loatin*/point nu)-er 1)J BB a S1ste). eci)al 3loatin*/point nu)-er This is so)ewhat i)portant -ecause so)eti)es 1ou )ust )atch a literal to the si*nature o3 so)ethin* or speci31 the value to <de3eat< an i)plicit cast -ehavior 1ou don<t li+e. Gor e:a)ple5 Hashta-le na)es @ new Hashta-le71NN5 N.19J won<t co)pile -ecause the constructor ta+es para)eters 7int5 3loat9 and the a-ove is 7int5 dou-le9. The line should read Hashta-le na)es @ new Hashta-le71NN5 N.139J 6 3ull listin* o3 the su33i:es is in the Rra))ar portion o3 the C# speci3ication 7appendi: 6 in the EC86 speci3ication5 appendi: C in the 8S speci3ication9. The su33i:es are also detailed in the Miterals section o3 the speci3ication 7;.$.$ o3 the EC86 speci3ication5 2.$.$ o3 the 8S speci3ication9. How do I use an alias 6or a na,espace or class? Fse the usin* directive to create an alias 3or a lon* na)espace or class na)e. Eou can then use it an1where 1ou nor)all1 would have used that class or na)espace. The usin* alias has a scope within the na)espace 1ou declare it in. Sa)ple codeC BB Na)espaceC usin* act @ S1ste).=unti)e.=e)otin*.6ctivationJ BB ClassC usin* list @ S1ste).Collections.6rra1MistJ ... list l @ new list79J BB Creates an 6rra1Mist act.Frl6ttri-ute 3ooJ BB E>uivalent to S1ste).=unti)e.=e)otin*.6ctivation.Frl6ttri-ute 3oo WhatCs the di66erence 0etween override and new? This is all to do with pol1)orphis). When a virtual )ethod is called on a re3erence5 the actual t1pe o3 the o-.ect that the re3erence re3ers to is used to decide which )ethod i)ple)entation to use. When a )ethod o3 a -ase class is overridden in a derived class5 the version in the derived class is used5 even i3 the callin* code didn<t D+nowD that the o-.ect was an instance o3 the derived class. Gor instanceC pu-lic class 2ase I pu-lic virtual void So)e8ethod79 I K K pu-lic class erived C 2ase I pu-lic override void So)e8ethod79 I K K ... 2ase - @ new erived79J -.So)e8ethod79J will end up callin* erived.So)e8ethod i3 that overrides 2ase.So)e8ethod. Now5 i3 1ou use the new +e1word instead o3 override5 the )ethod in the derived class doesn<t override the )ethod in the -ase class5 it )erel1 hides it. In that case5 code li+e thisC pu-lic class 2ase

I pu-lic virtual void So)e4ther8ethod79 I K K pu-lic class erived C 2ase I pu-lic new void So)e4ther8ethod79 I K K ... 2ase - @ new erived79J erived d @ new erived79J -.So)e4ther8ethod79J d.So)e4ther8ethod79J Will 3irst call 2ase.So)e4ther8ethod 7line 395 then erived.So)e4ther8ethod 7line $9. The1<re e33ectivel1 two entirel1 separate )ethods which happen to have the sa)e na)e5 rather than the derived )ethod overridin* the -ase )ethod. I3 1ou don<t speci31 either new or overrides5 the resultin* output is the sa)e as i3 1ou speci3ied new5 -ut 1ou<ll also *et a co)piler warnin* 7as 1ou )a1 not -e aware that 1ou<re hidin* a )ethod in the -ase class )ethod5 or indeed 1ou )a1 have wanted to override it5 and )erel1 3or*ot to include the +e1word9. That provides the -asics o3 overridin* and the di33erence -etween new and override5 -ut 1ou should reall1 see a -oo+ or tutorial 3or a )ore in/depth loo+ at pol1)orphis). Q: What is the difference between const and static readonly? A: The difference is that static readonly can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To e pand on the static readonly case a bit, the containing class can only modify it: ! in the variable declaration "through a variable initializer# ! in the static constructor "instance constructors if it$s not static# Q: %ow do & ma'e a ()) in *+? A: ,ou need to use the -target:library compiler option. Q: %ow do & use enum$s in *+? A: %ere$s an e ample: namespace Foo { enum Colors { BLUE, GREEN } class Bar { Colors color; Bar() { color = Colors.GREEN;} public s a ic !oi" #ain() {} } }

Q: Why is the compiler referencing things that &$m not telling it to reference?

A: The *+ compiler automatically references all the assemblies listed in the $csc.rsp$ file. ,ou can disable the usage of the csc.rsp file by using the -noconfig compiler option on your command line. .ote that the /isual 0tudio ..1T &(1 never uses the response file Q: (oes *+ support templates? A: .o. %owever, there are plans for *+ to support a type of template 'nown as a generic. These generic types have similar synta , but are instantiated at runtime as opposed to compile time. ,ou can read more about them here. Q: &s it possible to have different access modifiers on the get-set methods of a property? A: .o. The access modifier on a property applies to both it$s get and set accessors. What you probably want to do if you need them to be different is ma'e the property read!only "by only providing a get accessor# and creating a private-internal set method separate from the property. Q: %ow do & create a multi!language single!file assembly? A: This is currently not supported by /isual 0tudio ..1T. Q: %ow do & get deterministic finalization in *+? A: &n a garbage collected environment its impossible to get true determinism. %owever, a design pattern that we recommend is implementing &(isposable on any class that contains a critical resource. Whenever this class is consumed it may be placed in a using statement for e ample: usin$(File% ream m&File = File.'pen(()c*+ emp+ es . , ), File#o"e.'pen)) { in -ile'--se = .; /0ile(-ile'--se 1 m&File.Len$ 0) { Console.2ri e((c0ar)m&File.Rea"B& e()); -ile'--se 33; } When my2ile leaves

the le ical scope of the using, its dispose method will be called. Q: What is the difference between a struct and a class in *+? A: 2rom language spec: The list of similarities between classes and structs is longstructs can implement interfaces, and can have the same 'inds of members as classes. 0tructs differ from classes in several important ways, however: structs are value types rather than reference types, and inheritance is not supported for structs. 0truct values are stored on the stac' or in!line. *areful programmers can sometimes enhance performance through 3udicious use of structs. 2or e ample, the use of a struct rather than a class for a 4oint can ma'e a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 566 points. With 4oint implemented as a class, 565 separate ob3ects are instantiatedone for the array and one each for the 566 elements. Q: &s it possible to restrict the scope of a field-method of a class to the classes in the same namespace? A: There is no way to restrict to a namespace. .amespaces are never units of protection. 7ut if you$re using assemblies, you can use the $internal$ access modifier to restrict access to only within the assembly. Q: &s there regular e pression "rege # support available to *+ developers? A: ,es. The ..1T class libraries provide support for regular e pressions. )oo' at the documentation for the 0ystem.Te t.8egular1 pressions namespace Q: Why do & get a security e ception when & try to run my *+ app? A: 0ome security e ceptions are thrown if you are wor'ing on a networ' share. There are some parts of the framewor's that will not run if being run off a share "roaming profile, mapped drives, etc.#. To see if this is what$s happening, 3ust move the e ecutable over to your local drive and see if it runs without the e ceptions.

9ne of the common e ceptions thrown under these conditions is 0ystem.0ecurity.0ecurity1 ception. To get around this, you can change your security policy for the intranet zone, code group 5.:, "the zone that running off shared folders falls into# using the caspol.e e tool. Q: What is the e;uivalent to regsvr<: and regsvr<: -u a file in ..1T development? A: Try using 8egAsm.e e. The general synta would be: 8egAsm there$s a pretty good description of it and it$s associated switches in the ..1T 0(= docs. >ust search on ?Assembly 8egistration Tool?. Q:(oes *+ support parameterized properties? A: .o@ however, *+ supports the concept of an inde er from language spec: An inde er is a member that enables an ob3ect to be inde ed in the same way as an array. Whereas properties enable field!li'e access, inde ers enable array!li'e access. As an e ample, consider the 0tac' class presented earlier. The designer of this class might want to e pose array!li'e access so that it is possible to inspect or alter the items on the stac' without performing unnecessary 4ush and 4op operations. That is, 0tac' is implemented as a lin'ed list, but it also provides the convenience of array access. &nde er declarations are similar to property declarations, with the main differences being that inde ers are nameless "the name used in the declaration is this, since this is being inde ed# and that inde ers include inde ing parameters. The inde ing parameters are provided between s;uare brac'ets. 4ther G6?`sC

1)Pag level transaction how to maintain 2)How to group mutiple controls with a single validation control 3)what is http.pipeline 4)why do we need dataadapter what is the logical use of that 5)how do we implement caching in asp.net why it is called smart caching -- use cachedependency how the page nows that there is some change in the data!ase and it updated the page design patterns process "uality multiple page transaction #ew $e%uires#ew &"' ( )'*+, Providers s%l nested transaction -ml seriali.ation +ifference !etween /lient 0ctivated 1 &erver 0ctivated &ingle/all +ifference !etween 2e! &ervices 1 $emoting validator controls - what is the difference !etween 3e-t property and *rror4essage property validator controls - if 5avascript is disa!led6 will this validator controls wor 7 give reasons 8 0+).#*3 architecture difference !etween 0&P 1 0&P.#*3 difference !etween 0+) 1 0+).#*3 0ccept/hanges 1 $e5ect/hanges... 1)how to maitain sessions across we! garden9muliple cpu:s 2)what is remote host 3)how to create dynamic assem!ly 4)how to use e-ception in we! services 9soapframe) 5);nde-ers inside the loop will all the values !e displayed of the array <)how to get the element of the -mldocument =)is there any default login page in .net

>)what is polymorphomisn is there any other method !y which u can enforce polymorphism apart from using virtual and override ?)what is delegate9multicast delegate) 1@)can u use polymorphism in delegates 11)can i call a group of methods using delegates 12)how -sp wor er process is assocaited with session 13)what is isapi filters 14)what is la.y registration 15)how can i pass infinite parameters in the method 1 time i want to pass 2 parameters and ne-t time i want to apss 3 methods 1<)what are the different types of arrays what is the advantage of using 5agged array over rectangular array 1=)do u have to do any setup for using the validation in asp.net 1>)i have virtual methods in the !ase and overriden methods in the child class.now if i ma e reference of the !ase class and o!5ect of the child class what happens 1?)same %uestion without using overriden methods in the child class 2@)pu!lic class a9) A pu!lic int i B 1@@C D will it create a pro!elm 21);nde-ers program ma e a class which has an array and then ma e a class which has inde-ers and ma e the o!5ect of the inde-er
19)1-ase 29t1ped dataset 39dataset and recordset $9union in c# &9.oins and unions (9code 3or populatin* and )anipulatin* dataset ,9code 3or datevalidation 09Rac ;9user lo**in* in 3ro) di33erent location 1N93or)s and windows authenctication 119re)otin* 129what is stron* na)e 139transactions in dataset 1$9create a tree view structure usin* data*rid //////////////////////////////////////////////////////////////////////////////////// 19what is *ac 29what is sn+ 3ile 39how to create the sn+ 3ile $9where to use the sn+ 3ile in visual studio &9di33 -etween datareaders and dataset (9code to -ind the data*rid with datareader and dataset ,9i)ple)entin* relations and constraints in dataset 09how to do pa*in* in data*rid ;9what is the path o3 the *ac 1N9what is shadowin* and overridin* 119di33 -etween inter3aces and a-stract classes 129v-.net 3ile used in c# application 139transaction in .net 1$9code 3or usin* co) co)ponent in .net 1&9what is service co)ponent. 1(93or)s and windows authentication

what is operator overloadin*? 6ns C 4perator overloadin* ena-les developers to de3ine new struct t1pes that -ehave )uch li+e the prede3ined value t1pes. Gor instance5 a i*it struct can support the sa)e )athe)atical operations as the prede3ined inte*ral t1pes5 and can de3ine conversions -etween i*it and prede3ined t1pes. The prede3ined t1pes e)plo1 operator overloadin* the)selves. Gor e:a)ple5 the co)parison operators @@ and P@ have di33erent se)antics 3or di33erent prede3ined t1pesC a Two e:pressions o3 t1pe int are considered e>ual i3 the1 represent the sa)e inte*er value. a Two e:pressions o3 t1pe o-.ect are considered e>ual i3 -oth re3er to the sa)e o-.ect5 or i3 -oth are null. a Two e:pressions o3 t1pe strin* are considered e>ual i3 the strin* instances have identical len*ths and identical characters in each character position5 or i3 -oth are null. The e:a)ple class Test I static void 8ain79 I strin* s @ DTestDJ strin* t @ strin*.Cop17s9J Console.WriteMine7s @@ t9J Console.WriteMine77o-.ect9s @@ 7o-.ect9t9J K K produces the output True Galse -ecause the 3irst co)parison co)pares two e:pressions o3 t1pe strin*5 and the second co)parison co)pares two e:pressions o3 t1pe o-.ect. wh1 is 3inall1 -loc+ used? Wh1 is the na)espace S1ste).collection used? i33 -Bw 6rra1 and 6rra1list? What is a estructor ? 6ns C 6 destructor is a )e)-er that i)ple)ents the actions re>uired to destruct an instance o3 a class. estructors cannot have para)eters5 cannot have accessi-ilit1 )odi3iers5 and cannot -e called e:plicitl1. The destructor 3or an instance is called auto)aticall1 durin* *ar-a*e collection. What is a instance constructor? 6ns C 6n instance constructor is a )e)-er that i)ple)ents the actions re>uired to initialiLe an instance o3 a class. What is a Static Constructor? 6ns C 6 static constructor is a )e)-er that i)ple)ents the actions re>uired to initialiLe a class. Static constructors cannot have para)eters5 cannot have accessi-ilit1 )odi3iers5 and cannot -e called e:plicitl1. The static constructor 3or a class is called auto)aticall1. what is static =eadonl1 3ields ? 6ns C Static 3ields are not a per3ect )atch 3or this scenario. The 3ields are initialiLed at so)e point -e3ore the1 are used5 -ut a3ter this initialiLation there is nothin* to stop a client 3ro) chan*in* the). Such a )odi3ication could cause unpredicta-le errors in other pro*ra)s that use Color and assu)e that the values do not chan*e.

=eadonl1 3ields can -e used to prevent such pro-le)s. 6ssi*n)ents to a readonl1 3ield can onl1 occur as part o3 the declaration5 or in an instance or static constructor in the sa)e class. 6 static readonl1 3ield can -e assi*ned in a static constructor5 and a non/static readonl1 3ield can -e assi*ned in an instance constructor. Thus5 the Color class can -e enhanced -1 addin* the readonl1 )odi3ier to the static 3ieldsC class Color I internal ushort redSartJ internal ushort -lueSartJ internal ushort *reenSartJ pu-lic Color7ushort red5 ushort -lue5 ushort *reen9 I redSart @ redJ -lueSart @ -lueJ *reenSart @ *reenJ K pu-lic static readonl1 Color =ed @ new Color7N:GG5 N5 N9J pu-lic static readonl1 Color 2lue @ new Color7N5 N:GG5 N9J pu-lic static readonl1 Color Rreen @ new Color7N5 N5 N:GG9J pu-lic static readonl1 Color White @ new Color7N:GG5 N:GG5 N:GG9J K What is a constant? 6ns C 6 constant is a class )e)-er that represents a constant valueC a value that can -e co)puted at co)pile/ti)e. Constants are per)itted to depend on other constants within the sa)e pro*ra) as lon* as there are no circular dependencies. The rules *overnin* constant e:pressions are de3ined in constant e:pression The e:a)ple class Constants I pu-lic const int 6 @ 1J pu-lic const int 2 @ 6 ' 1J K shows a class na)ed Constants that has two pu-lic constants. Even thou*h constants are considered static )e)-ers5 a constant declaration neither re>uires nor allows the static )odi3ier. Constants can -e accessed throu*h the class5 as in class Test I static void 8ain79 I Console.WriteMine7DINK5 I1KD5 Constants.65 Constants.29J K K which prints out the values o3 Constants.6 and Constants.2. What is a )ethod? 6ns C 6 )ethod is a )e)-er that i)ple)ents a co)putation or action that can -e per3or)ed -1 an o-.ect or class. 8ethods have a list o3 3or)al para)eters 7which )a1 -e e)pt195 a return value 7or void95 and are either static or non/static. Static )ethods are accessed throu*h the class. Non/static )ethods5 which are also called instance )ethods5 are accessed throu*h instances o3 the class. 8ethods can -e overloaded5 which )eans that )ultiple )ethods )a1 have the sa)e na)e so lon* as the1 have uni>ue si*natures. The si*nature o3 a )ethod consists o3 the na)e o3 the )ethod and the nu)-er5 )odi3iers5 and t1pes o3 its 3or)al para)eters.

The si*nature o3 a )ethod does not include the return t1pe. What is a Sropert1 ? 6ns C 6 propert1 is a )e)-er that provides access to a characteristic o3 an o-.ect or a class. E:a)ples o3 properties include the len*th o3 a strin*5 the siLe o3 a 3ont5 the caption o3 a window5 the na)e o3 a custo)er5 and so on. Sroperties are a natural e:tension o3 3ields. 2oth are na)ed )e)-ers with associated t1pes5 and the s1nta: 3or accessin* 3ields and properties is the sa)e. However5 unli+e 3ields5 properties do not denote stora*e locations. Instead5 properties have accessors that speci31 the state)ents to -e e:ecuted when their values are read or written. What r dele*ates and events? 6ns C It su33ices to si)pl1 state that a dele*ate is the o-.ect/oriented 7and t1pe/sa3e9 e>uivalent o3 a 3unction pointer. 6 dele*ate instance holds a re3erence to an instance or class )ethod5 per)ittin* anon1)ous invocation o3 the -ound )ethod. Events are special constructs declared on a class that help to e:pose state chan*es to interested o-.ects at run ti)e. 6n event represents a 3or)al a-straction 7supported -1 the CM= and various co)pilers9 o3 the re*istration5 unre*istration5 and noti3ication )ethods we used previousl1 to i)ple)ent the 4-server pattern. ele*ates are re*istered with speci3ic events at run ti)e. When the event is raised5 all re*istered dele*ates are invo+ed so that the1 receive noti3ication o3 the event. 19What is usercontrols? 29Can we have d1na)ic usercontrols and can we pass values 3ro) the usercontrols to the asp: pa*es? 39What is the di33 -etween a-stract class and inter3ace $9Can u use .avascript in asp: &9what is the di33 -etween windows dll and co) dll (9 i33 -etween asp and asp.net ,9 i33 -etween havin* and where clause 09Can i have )ulitple *lo-al.asa 3iles ;9 i33 -etween )achine.con3i* and we-.con3i* 1N9what is we-.con3i* 119T1pes o3 para)eters passed in c# )ethods 129-1re3 and -1val 139di33 -etween repeater5datalist and data*rid 1$9threadin* 1&9can i have a connected dataset 1(9wh1 u need dataadpater 1,9di33 -etween servercontrols and ht)lcontrols 109what is response.end5response.clear and response.3lush 1;9what is di33 -etween asp cachin* and asp.net cachin* 2N9what is *lo-al asse)-l1 cache.how to put the co)ponents in the R6C and is there an1 steps -e3ore that 219CM=5CTS and CMS 229what are stron*na)es 239what all u can pass in -1val and -1re3 2$9what is asse)-l1 2&9what is na)espace and can we have nested na)espace in a dll and where e:actl1 the na)espace is stored 2(9what is the version control in .net 2,9what is the disadvanta*e o3 usin* static 2096part 3ro) 48 how can u retreive records in T8M 2;9how to use co) in .net and vice versa 3N9oops5operator overloadin*7What happend when the return t1pe is di33erent and the si*nature is the sa)e9

319What is the di33erence -etween operator overloadin* and overidin* 329What is the na)espace used 3or creatin* the collection o-.ect 339can u 3orce the *ar-a*e collector 3$9can u use tr1 without catch 3&9how to create disconnected recordset in asp 3(9what is the process 3low o3 asp.net pa*e 3,9what is the pro-le) o3 usin* server.transer 19what is :)ldatadocu)ent and :)ldocu)ent 29what is response.-u33er 39can i have )ultiple we-.con3i* 3iles in the application $9can i use .avascript in the we-servercontrols &9how to access server varia-les inside .avascript (9what is the di333 -etween ht)lservercontrols and we-servercontrols ,9how to con3i*ure s)tp server 3or sendin* )ails 09can i use varia-le in the include 3ile ;9what is the di33 -etween server.trans3er and response.redirect 1N9how is the version is )aintained in the asse)-l1 119how can i re3er to an asse)-l1 with a di33erent version 129can i use action@Da.e:eD inside the 3or) ta* 139can i use server.createo-.ect7a.e:e9 1$9can i pass >uer1strin* in server.trans3er 1&9di33 -etween server.trans3er and server.e:ecute 1(9what is the step to -e done -e3ore )a+in* an instance o3 e:cel 3ile usin* server.createo-.ect 1,9what are t1pe datasets 109what is the di33erence -etween )ani3est and )etadata .net 3ra)ewor+ di33 -etween asp and asp.net di33 t1pes o3 asse)-lies -o:in* and un-o:in* di33 -etween classes and structs5 what to use when di33 t1pes o3 .oins di33 -etween in clause and e:ists di33 -etween truncate and delete what are the various )ethods -1 which u can populate dataset apart 3ro) dataadapter

You might also like