0% found this document useful (0 votes)
27 views

Mod 3

Dotnet module 3

Uploaded by

fakemefakerules
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
27 views

Mod 3

Dotnet module 3

Uploaded by

fakemefakerules
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
Hod - 3, Asstgoment No Jt i]] + waite a methed “that can accept any durdbercok orgurpents by wuing the. params _Kegioor! 4) + The “ params* s beyword fo GE allows you -to_crecute + ‘emethods that + cans, accept: O .Vaxtable: | nurober of PorarnetyS Of -the | Sance' types : 4) “the Prams! Keyworel enabies a metros fo accept as asia curobee ‘oy Larguenents. oh che | Speeifred -type. De provider a way “call _metbods with a vortatle, number of gunmen! withoud explicit yy Crentifg ap array to hold Hose ovqueceuss ni 9203\ ~ using System 4 Mamespa ci Access Speci Pier: a 2 Proqrars : ran; { for Cint i=0; Pc val. Length ; ttt) 2 2 . 4 Console, Wait Line(_val til); A 6 J & % Ces way . 10 Static void Nain (String t)_ogs) 2 jean ree ; a Progvarm pragram = neu Programm)” og Yares Show (9,462, 10 12,10)» , : * ‘params! 9 ow ee _ 2 ‘ i Ge the mal eee oe multiple iokger argumuss and -then terete: toouga she provided orgements Degine paras -hrray «List out -Ihe_ estrictiong on _pouste arrays will exaenple, toro ya +] “the Posoms keyword io C4) allows you 4 i create method | -Wodt Can || icitcept a, vawable number of pouamerers of, ~the same, ty pe. A) the [wowing List contains the estrictins of am arrays. pera. otra i) We Cannot _ use" the params ‘hey wooad com mudtiotinens ional — orray. Ee public stance _ int Hin vo Cpmacos int [1 table. ML toropile Herne error 2\ we can not overload a method basal on tbe pouaros hepuoogel. & "public static tot Mig ( int LJ parambist) whic State int Mir cuams jot! List) [lecror + duelicate detlasation 2} we au not allowed -to Specify cthe ref,’ out modifi with params oxcra.y Ey public Static int Min C vet params int C7 Posamust) HM exvor coroptie time error. pro U]_ A params arrqy must be the last 1 pesorpese, : —| Ex: public. statto Tot eer int Pararetin | + fot) Jlexcors coral ‘tine error, 5)-* non-params method aloay eee spar overs & Param: med. Ey pubite Stabe tot Mio Cine tet tnt atgtt) [ Public Static lot MinC parames tnt oO parar Ust) -Désigoryent We 12 Explain now to create a derived class “thet Toberlts features from a bare: class | eit example: +) Jo Cit. inheritance ts a! mechaniss iwbere a New. Class Kw asi _arderived class (Cao tober attributes ¢ bebaviors fom ap existing class Koon as dbase class. +#) In cH: > inbeitance | Cans be achieved Laing abe! :' Syma) Lollowed by the. narce of Bell bore Class when defining -the dlevived = Ex Want : : To Bose Class 2 ny Saat class -Animol 4 I[ Derived class public closs\ Dog! Animal : > & U ; hoe public void Barc) q : : Console. WetteLine (the dog is parting.) ; t a J: 3 _class Peegraro iG tote: void Main Cstringl3 args) i t ; ge orgs) Deq_-dag = oe0o Deg “clogunaen SS Bade ; a 12190 Faser Consoleswarie bine C'Deg's_naroet' + log. 0ere) 5 ia BogaiEorC 2 clog. Bark C7 } 4 aint 4 In aos exarople, ) Bose Class C'Anfed') > ' +) Tt: defines a ‘bosie' class “nimnal’ wtb & property Warne! anda mettoed | Ears 2. Derived. Class Cirpeg) s+ #) 1tls _declmeds! cot) fhe “2 -Aviroal! Syotae fodicating ~tho4 “Dog Toherfts from “Aovena)". AD Coherits the: Mame! | propery aod “the ‘Gato roetbad from thes Anima)’ class aod ad8s0 is ‘own — roetbod! - "Bax CD" 37 Maio Method : 2k) “Creates, an “fastance, of he ‘Deg’ Class dy ah Kh) Sets Se ‘Kame’ proputy of the leg. *) calls abe ‘EatO' | metbad ioberitéel' trom sie bare loss. *)_calls the ‘Barbed! mmettod. defined s fy the Cleréved class’ un , fi i Gg Define \ faben'tances_, Expla} trom an existing class. Sssigorment Nols a ‘pau _metboas virtual L_ovemide methads with: example’ to -_fohertance it eoethod In cH, toheritance ts “the. aloi bit. +2. Create o% class that _fherits _at-tv! loudes am behaviors = virtecut Hettod: : *I A vivtea) metoed ig ia _mefbed fo _o bare class fiat Can be overridden i derived, classes. A) Tt provde a default fey plementation rat ‘Qn be’ overridden ag derived: Classes +) Te allows polyrmor phiC _behavider, Meaning tHe methed Called depends 00 the attdad type of tbe object ot yuntinne. + Hoey ~*} We use the. Keguoord - "virtual! } 2 override _Meltet 2 +) Ag overnite method is ail 'rhettid fa dertued “class | that replaces: Hex implementatt’ sn OL virtaod method fo toe baw class +) Tt must have tee Same ajgnatute Coame retain type ge paariicra) “Os +6e victuals Meteed it overnite *) Te provide a Sect Gated implementation for chp thethed! ialehing! to dhe boxe class, i *)we vse fe keyword Soverride!. ¢ Gere 3 using usterr 5 4 BY los \ 4 £ a path icteal vote a Hout dascwype7a consple Writelice C" This ts the ba ‘clasg metiiod'') 4 3 Class DevivedClass ¢ BaseClass weeblic- override | vod Display) 7 : Dk Console. Wrideline C'This s\ Hie derived’ Class ‘roethod!) y 3 wed Class Proqvom. ¢ ; State void MainC sting C) a”gs) £ Case Class | bareobf = nee BaseClassC)} basobs'.DisplayC): Novixpecht This ts bate ‘clay metbod DerivedClass. derivedobf= neo DerivedCless cy’ derived Obf, Displa yC); [loctpet's This (S the derited class met GaseClass deriveReP = nero Derived Class(); erivedRel. Dis platy; J eeiaell y t 3 +) 8 this exorple, 'DisplayU ‘sa viv tual metbod. fo the @axCloss am fe fs overridden fo the _“‘DerivedClase., cohen: Sou Call displayC) On an objet of the Derived Class | ibe overnivae method in the Porived class. is ex enced. Even ip. toe’ reference is of the base Class =tyee but “the objet is i ol the derived class type. =the overridden method in the akrived Class will be Called. pssigoment Mo re Implement an rier face toa Explain how to stuctuae or Class wit excaro ples» To iroplement a0 foterface fo a structuee or Class ce, follow terse steps De claus -tben'Tnterface witty "} Define be _inter face 8 ts you want the iraplemectig sihe method Signet i) wClass..or structure to ‘provide 2) Implement the Torr face + Dmplerent toe FoterBce fo_yout. class ov stracture by providing Concrete Teoplerpentations for atl lhe _roetbods declared foe _ forterface. p ; nA 3) Use he PaierPaces Create obseets Sof your (las9 or Structuse and. frteroct whit ery “throug the! frerface: velwence : aa Droplementing On‘ vortrPac 10 aw « lass woh t Using Systeno? fotergace TExcimple : eit £ void Displaycrs 5 v i , - gas Myjclass: ': TExample. public; void Display & “Console, Waricline C "Implementation of Dis plex Petod gn! Myclas"D: vid: i 4 class Prograra & Hor vol Mout ‘ f 1 Stafio: = volt Main € ste ogo acgs) - ¢ rt Hyclass Obj = ew Myclass Os bp Ds plays 24 ~ Ayesvt ape ef be 1 \ ro plementing Qo _inkrface fe a etructuy . sing = Systeroj. interface \ TE vamelé : A ih 4er3_\heplay Oe 1 Ban Shutk | Mysiruch? Dexoropens th public void Display Y shail & Console, Waiteline C" Display meteod _implermente! j eg“ MyStecrct"") 5 j class Program £ Stotic void Maio Cstringl3_ args) { My Strack obf = hew MyStructc) ; obs" Diselay cd; PIO J In the above both Cece: A) we degine an ink face 'L€xample’ witb a roctbod ‘Displey CO" #) we Create a Struct ‘Myctuc " and aso created eclass 'Mycloss' ahd both axe + tro pleroenteo! in the ‘IEcample' interface fy providing a> implementation for “the ‘Display’ metbod. 4) Jo the ‘Main’ metbod, we creede an Instance of both _'MyStract" B 'myCtass' and calls its Bisplay6’ $ method which prints the warte,Uee, to tbe console, what is toter face 4) In ctf, an interface is a'icontract +Hhat defines a set of medthady & moopesses without sroplementect'on. ASS'GOment N90 0.3 ‘6 whet fs" need foc virtucd Wetton Spletn with an: example. + Virtual Panchen [virtual metbod.. +) virtual retheds fo cop: allow aba class -to__ provide a clefautt ienplementation Of a _methcd -that Con be overridlcko by Gerived classes, A) A metic! thet is Potended +o be’ overridden fs called a _virteal method, = A) we cap mark a methed as a Virtuad pettoad by wing tbe virtual Kesyscorel. A] we shoul Know “He clifterences between overnideling & method & biddag a metbal overriding 2 provides A new implementation of a + method in G Subclass. csbile maizteining “Kee metbal y origi ol fun ronal ty ing : “Indroducts a Nero metnod ort the Same bame “in a Subclass, Unrelatet -to Be mettcd f “the superclass | which Ps discouraged o P+) Tt provides aplexi bier ty for Swroctassey te implement methods Speuife -te heir Contect. A) de __ Supports pelyenerpbises by allowing metied, pb .- . WV je be overridden -to Suit Varying ‘emg eu requirerents. PTO \ — 2D Brcurpple 2 \Tostning -metbiel im * System, Objet! Namespace Systems ft ‘ » class Obpeet ve { Identify the vse of extension methods 1 cH with programming examples #) Extension methods in cH ollow you to add roetnods do existing ty pes__ without moditytog xthers. Source code. ve) Uses 04 Extension methods \_ enbance existing Eypes 2y Improve Code Readaboidity Sy Pacibitase: LING. ex tension method in a_ Static +) Define a0 + class. using dfs Keyword «or the “first “pata meter eto, Specify che type belng — extended. EF State class» Uti) ¢ 2. public stable Int Negak ( this int t) a vy tse dt seturg -i} +o 5 A) “you cay call the . retbod using dot _ootation s I> tot x= 5917 Console. waitlive C Wegatec)) Jloutpar !- Fas +) you dont need +o xeference - the Ue! clas. atcally reco rnizes extensim method *) you Can also Call -this method wstog Console. Weiteline C Util. Negacte ©) Jlouput 2-591 gil Develop» a __c# “Code to demonstrat? multiple. : interfaces 5 q 3 +J A class cay bave at most. one: base ctoss A) But, it ts allowed +o implement an Unlimited nanmber of, Prterfaces. 4) _Dermo o tratiqg a class 2 tepplemenitéo, gute iple. 0 i (- public _toteface TPeint { void print tr; } bol oe 3 TP ris 4 public void priotc) ¢ console. wateline(*peitieg')t } ‘ ‘ eee lagt)s ¥ J ou CLo.ss—Peasyra.cn public ste void Movin CStripflorgs) { y* Machine m= new Machine C)*Y ro.print C). + rm. Scan C) > ‘/f'Scai ing “by S| Explain how fo ronnnges — Sysien_aesounces fey wsing garbage. collection unto ~ exarnple - . ~ *) Tp aye gow bage: collection tsa ey’ featute oy © &__SySttry *NET 2undime sfoc_Ma: aneyiog ana 9 WSOurces | 1 : 2 Te helps 4 cucermatenly handle he eSlocatien 2; —sealocadion of. cee oy tose he Jing. Ypoints_will explatsi bela Manageme workS wi-ty garese Coll ection s : i: ‘]_etomatio, Memey Manegecmt | a. objece Cas. be. des toyed Q tts: EMOrY epee Avaikable tor seuse only When all the’ seferences cto it bover disappeared oe) =: 127 Generation a} Gaabape: Salletion Desoutey +) Generation Ot . Sort Lived Objet, frequentty collexted t *) Generation 1s Objects Suvived fo eno 4. less Spequesty, Collectec 4) genecation 2: tonq-tied objets £ efit otal oi Collected ~* v Ji ~Pesltan #) Dispose Pacttero xf #o_ shy 03 ‘ —) cH vses UDisposrable interface g Dispose Method for _wmana aging senoutnés » ett A S]- Ges Collectt) Method wD you can _manwedlyn trigga iGo wing ge. collectC) & this fess, collection 04 atl generations. 2 ‘ -*)_ Ao“ exarnple using System: niteitt public class R% TDispositle ¢ ‘ public void Dispace C) { _¢c, Suppress Prnalize (this) | neh ones, -PC) > Dispose t>: 5 ‘ public Class © < public iS VMCSTIA) a A 4 USing Cras r= neo RC) ! L v 2 Uitge 228 Ou1oe b @L£, Collect C) uj Discuss: toter face: restrictions iq CH A) The essential tdea to remember (8 “that an Les fesse never» fontans + any: ea +) the following fist Contains Ibe Toke 201 trichony D_we're not allowed: to define any field to 20 lniteface, not even | Stanic field poe | 2) we're not allowed +o idetine any Constructors in an fotecface 3) we're. not * alloweel .

You might also like