Author Archives: Sandeep Rana

Chapter110:Timers

Section110.1:MultithreadedTimersSystem.Threading.Timer-Simplestmultithreadedtimer.Containstwomethodsandoneconstructor. Example: A timer calls the DataWrite method, which writes "multithread executed…" after five seconds have elapsed, and then every second after that…
Continue reading

Chapter109:Stream

Section109.1:UsingStreams Astreamisanobjectthatprovidesalow-levelmeanstotransferdata.Theythemselvesdonotactasdata containers. Thedatathatwedealwithisinformofbytearray(byte[]).Thefunctionsforreadingandwritingareallbyte orientated, e.g. WriteByte(). Therearenofunctionsfordealingwithintegers,stringsetc.Thismakesthestreamverygeneral-purpose,butless simpletoworkwithif,say,youjustwanttotransfertext.Streamscanbeparticularlyveryhelpfulwhenyouare dealing with large amount of data. WewillneedtousedifferenttypeofStreambasedwhereitneedstobewritten/readfrom(i.e.thebackingstore). Forexample,ifthesourceisafile,weneedtouseFileStream: stringfilePath=@"c:\Users\exampleuser\Documents\userinputlog.txt"; using(FileStreamfs=newFileStream(filePath,FileMode.Open,FileAccess.Read, FileShare.ReadWrite)) { //dostuffhere... fs.Close();…
Continue reading

Chapter108:CheckedandUnchecked

Section108.1:CheckedandUnchecked C#statementsexecutesineithercheckedoruncheckedcontext.Inacheckedcontext,arithmeticoverflowraisesan exception. In an unchecked context, arithmetic overflow is ignored and the result is truncated. shortm=32767; shortn=32767; intresult1=              checked((short)(m+n));                //willthrowanOverflowException intresult2=              unchecked((short)(m+n));//willreturn-2…
Continue reading

Chapter107:Indexer

Section107.1:Asimpleindexer classFoo { privatestring[]cities=new[]{"Paris","London","Berlin"}; publicstringthis[intindex] { get{ returncities[index]; } set{ cities[index]=value; } } } Usage: varfoo=newFoo(); //accessavalue stringberlin=foo[2]; //assignavalue foo[0]="Rome"; ViewDemo Section107.2:Overloadingtheindexertocreatea SparseArray…
Continue reading