Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

C# AND .NET FRAMEWORK METHODS Objects encapsulate data, and code to manipulate that data.

The code designed to work on the data is known as methods in C#. DECLARING METHODS Methods are declared inside the body of a class, normally after the declaration of data fields. The general form of a method declaration is: modifiers ty e method!"me #form"$ "r"meter $ist% & method'(ody ) *ASSING *ARAMETERS TO METHODS hen you pass a parameter to a method, it is important to know what is going on in the background in order to a!oid une"pected results. #or e"ample, you may create a method to change the !alue of a !ariable, and then find out in the end that the change affected only the local !ariable and not the original !alue. One common e"ample of this problem is swapping the !alues of two !ariables. $n C#, there are two ways to pass a parameter to a method: by !alue %which is the default& or by reference. 'assing parameters by reference makes the changes to the !ariable !alues persist. To pass a parameter by reference, modify the parameter with the ref keyword. The following e"ample demonstrates swapping the !alues of two !ariables. E+AM*LE ,, SWA* METHOD E+AM*LE -si!. System/ 0$"ss MyC$"ss & st"ti0 1oid S2" #ref i!t 34 ref i!t y% & i!t tem 5 3/ 3 5 y/ y 5 tem / ) st"ti0 1oid M"i!#% & i!t 3 5 67/ i!t y 5 88/ Co!so$e.WriteLi!e #9:efore s2" i!.; 35&<)4 y5&=)94 34 y%/ S2" #ref 34 ref y%/ Co!so$e.WriteLi!e #9After s2" i!.; 35&<)4 y5&=)94 34 y%/ ) ) O>T*>T;

C# AND .NET FRAMEWORK :efore s2" i!.; 35674 y588 After s2" i!.; 35884 y567 The S2" method is using the ref keyword to modify the parameters in both the method header and the method call. This method did swap the two !ariables, as indicated in the output. $n the following e"ample, take a look at how this function will beha!e if you pass the parameters by !alue( without the keyword ref. E+AM*LE ,, SWA* METHOD E+AM*LE -si!. System/ 0$"ss MyC$"ss & st"ti0 1oid S2" #i!t 34 i!t y% & i!t tem 5 3/ 3 5 y/ y 5 tem / Co!so$e.WriteLi!e #9?"$-es i!side the method; 35&<)4 y5&=)94 34 y%/ ) st"ti0 1oid M"i!#% & i!t 3 5 67/ i!t y 5 88/ Co!so$e.WriteLi!e #9:efore s2" i!.; 35&<)4 y5&=)94 34 y%/ S2" #34 y%/ Co!so$e.WriteLi!e #9After s2" i!.; 35&<)4 y5&=)94 34 y%/ ) ) O>T*>T; :efore s2" i!.; 35674 y588 ?"$-e i!side the method; 35884 y567 After s2" i!.; 35674 y588 )s you can see in the output, the !alues of " and y did not change after in!oking the S2" method. The change took place only inside the method, but when the method returned to the caller, the !alues were the same. ?ARIO>S WA@S TO *ASS *ARAMETERS TO METHODS There is more than one way to pass parameters to a method by reference. There are three parameter modifiers, each of which results in sa!ing the changes of the !ariable !alues after the method returns to the caller. The three modifiers are: ref o-t A "r"ms

C# AND .NET FRAMEWORK >SING REF The ref keyword is used to pass !ariables to a method and reflect back the changes that occurred on the original !ariables. hen you use ref to pass a !ariable to a method, the !ariable must be initiali*ed first+ otherwise, you will get a compiler error. E+AM*LE; stri!. my?"ri"($e 5 9This my stri!.9/ ,, i!iti"$iBe the 1"ri"($e MyMethod#ref my?"ri"($e%/ ,, i!1oCe the method hen the method returns the !ariable, my,ariable will retain the changes that occurred to it in MyMethod. >SING O>T The o-t keyword does the same work as the ref keyword when used as a parameter modifier. $t does not re-uire initiali*ing the !ariable before passing it to the method, but it does re-uire initiali*ing the !ariable in the method itself. E+AM*LE; 1oid MyMethod#o-t stri!. my?"ri"($e% & my?"ri"($e 5 9This is my stri!.9/ ) $n order to call this method, use the o-t keyword: MyMethod#o-t my?"ri"($e%/ The !ariable my,ariable might not be initiali*ed at all. $t will be assigned the !alue .This is my string. when the method returns to the caller. /otice that the o-t keyword is used in both the method header and the call. $t is possible to use more than one parameter, as in this e"ample: 1oid @o-rMethod#o-t i!t 34 o-t i!t y4 o-t i!t B% & ... ) This method is called in the same way: @o-rMethod#o-t m4 o-t !4 o-t $%/ $n the following e"ample, the o-t keyword is used to pass an uninitiali*ed array to the method MyMethod. hen the method returns to M"i!, its elements are initiali*ed. E+AM*LE ,, O>T E+AM*LE

C# AND .NET FRAMEWORK -si!. System/ -($i0 0$"ss MyC$"ss & -($i0 st"ti0 1oid MyMethod#o-t i!tDE myList% & myList 5 !e2 i!tDE &=FG74 =FHH4 =FIJ4 =FFJ)/ ,, i!iti"$iBe the "rr"y ) -($i0 st"ti0 1oid M"i!#% & i!tDE my"rr"y/ ,, de0$"re "! -!i!iti"$iBed "rr"y MyMethod#o-t my"rr"y%/ ,, "ss it "s " "r"meter ,, Dis $"y the "rr"y; for #i!t i 5 < / i K my"rr"y.Le!.th / iLL% Co!so$e.Write#9&<) 94 my"rr"yDiE%/ ) ) O>T*>T; =FG7 =FHH =FIJ =FFJ

>SING *ARAMS The keyword "r"ms is used with arrays. $t lets you pass any number of parameters to a method without the need to declare them in an array. This keyword is re-uired only in the method declaration. #or e"ample, the method: st"ti0 1oid MyMethod# "r"ms o(Me0tDE myO(MArr"y% can be called like this: MyMethod#=684 NAN4 9My ori.i!"$ stri!.9%/ The parameters passed in this call are all of the type object, which means they can include any types descended from the object class. Consider also the following method: st"ti0 1oid MyMethod# "r"ms i!tDE myI!tArr"y% This method can be called by passing a group of integers that constitute an integer array regardless of its length, for e"ample: MyMethod#64 G4 J%/ )ny method that uses the "r"ms keyword cannot use more than one parameter.

C# AND .NET FRAMEWORK $n the following e"ample, the use of the "r"ms keyword in the declaration of the method is demonstrated. $t also demonstrates the o!erloading of two methods with the same name and different parameters. One method uses an object array and the second method uses an integer array. 0oth methods change the content of the array passed to it. The content of each array is displayed before and after the change takes place inside the method. E+AM*LE ,, *ARAMS AND O?ERLOADING E+AM*LE -si!. System/ -($i0 0$"ss MyC$"ss & ,, De0$"re MyMethod th"t -ses i!te.er "r"meters; -($i0 1oid MyMethod# "r"ms i!tDE myI!tArr"y% & ,, Dis $"y the i!te.er "rr"y (efore the 0h"!.e; Co!so$e.WriteLi!e#9My ori.i!"$ i!te.er $ist;9%/ for #i!t i 5 < / i K myI!tArr"y.Le!.th/ iLL% Co!so$e.WriteLi!e#myI!tArr"yDiE%/ Co!so$e.WriteLi!e#%/ ,, Ch"!.i!. the se0o!d "rr"y e$eme!t; myI!tArr"yD=E 5 777/ ,, Dis $"y the i!te.er "rr"y "fter the 0h"!.e; Co!so$e.WriteLi!e#9My i!te.er $ist "fter the 0h"!.e;9%/ for #i!t i 5 < / i K myI!tArr"y.Le!.th/ iLL% Co!so$e.WriteLi!e#myI!tArr"yDiE%/ Co!so$e.WriteLi!e#%/ ) ,, De0$"re MyMethod th"t -ses o(Me0t "r"meters; -($i0 1oid MyMethod# "r"ms o(Me0tDE myO(MArr"y% & ,, Dis $"y the o(Me0t "rr"y (efore the 0h"!.e; Co!so$e.WriteLi!e#9My ori.i!"$ o(Me0t $ist;9%/ for #i!t i 5 < / i K myO(MArr"y.Le!.th/ iLL% Co!so$e.WriteLi!e#myO(MArr"yDiE%/ Co!so$e.WriteLi!e#%/ ,, Ch"!.i!. the third "rr"y e$eme!t; myO(MArr"yD6E 5 9My !e2 stri!.9/ ,, Dis $"y the res-$ts "fter the 0h"!.e; Co!so$e.WriteLi!e#9My o(Me0t $ist "fter the 0h"!.e;9%/ for #i!t i 5 < / i K myO(MArr"y.Le!.th/ iLL% Co!so$e.WriteLi!e#myO(MArr"yDiE%/ Co!so$e.WriteLi!e#%/ ) ) 0$"ss M"i!C$"ss & st"ti0 1oid M"i!#% & ,, De0$"re "! o(Me0t "rr"y;

C# AND .NET FRAMEWORK o(Me0tDE myO(MList 5 !e2 o(Me0tDE &=684 NAN4 9My o$d stri!.9)/ MyC$"ss m0 5 !e2 MyC$"ss#%/ ,, *"ss fo-r i!te.ers to the 9first9 MyMethod; m0.MyMethod#==4 664 884 GG%/ ,, -si!. !-meri0 "r"meters ,, *"ss "! o(Me0t "rr"y to 9se0o!d9 MyMethod; m0.MyMethod#myO(MList%/ ,, -si!. "! o(Me0t "rr"y ) ) O>T*>T; My ori.i!"$ i!te.er $ist; == 66 88 GG My i!te.er $ist "fter the 0h"!.e; == 777 88 GG My ori.i!"$ o(Me0t $ist; =68 A My o$d stri!. My o(Me0t $ist "fter the 0h"!.e; =68 A My !e2 stri!.

You might also like