1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespacePractice18 {9 class ProgramTen { One--------------------------------------------------------------------------------------- A #使用一般方式, Exchange values for two variables - Static voidMain (string[] args) - { the #regionDeclaration of two variables: int n1=10, n2=20; Requires the exchange of two variables, the last output n1=20,n2=10, the extension (*): Do not use a third variable how to Exchange? - intN1 =Ten, N2 = -; - - #常规的交换数据方式 + inttemp=N1; -n1=N2; +N2=temp; A at #数学技术来交换数据方式 -N1 = n1 +N2; -N2 = N1-N2; -N1 = N1-N2; - -Console.WriteLine ("n1="+N1); inConsole.WriteLine ("n2="+n2); - Console.readkey (); to + #endregion - } the------------------------------------------------------------------------------------ * #使用封装, to handle the value exchange of two variables $ Static voidMain (string[] args)Panax Notoginseng { - #regionUse the method to achieve: encapsulate a method to do, the method has two parameters for num1,num2, the NUM1 and num2 Exchange. Hint: The method has two parameters N1, N2, in the method to exchange N1 with N2, using ref (*) the + intN1 =Ten, N2 = -; A theSwap (refN1,refn2); + -Console.WriteLine ("n1="+N1); $Console.WriteLine ("n2="+n2); $ Console.readkey (); - - #endregion the } - Wuyi Private Static voidSwap (ref intN1,ref intn2) the { - //throw new NotImplementedException (); Wu intTMP =N1; -N1 =N2; AboutN2 =tmp; $ } --------------------------------------------------------------------------------------- - } - } A
01Net Foundation strengthens the first day-Foundation strengthening little exercises