Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C# - out parameters and reference parameters
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 /* BEFORE PROCEEDING WITH THIS EXAMPLE,YOU'RE HIGHLY RECOMMENDED TO SEE THE CODE OF A C# METHOD THAT ACCEPTS AN INDEFINITE NUMBER OF PARAMETERS HERE: http://rextester.com/ICWH47686 */ using System; namespace Rextester { public class Operations { public static void SumUsingRef(ref int result,params int[] numbers) { for (int i=0;i<numbers.Length;i++) { result += numbers[i]; } } public static void SumUsingOut(out int result,params int[] numbers) { result=0; //when using "out" parameters,you should initialize all the out parameters for (int i=0;i<numbers.Length;i++) { result += numbers[i]; } } } public class Program { public static void Main(string[] args) { int result1=0; //note that in reference parameters,you might initialize the variable with any value you want Operations.SumUsingRef(ref result1,9,8,7); Console.WriteLine(result1); int result2; //here,even if you initialized result2 with any value you want,the method will always initialize it to zero Operations.SumUsingOut(out result2,9,8,7); Console.WriteLine(result2); } } }
run
|
edit
|
history
|
help
0
1
EFC AZAAAA
mendozaeject
Problem: on_off
সদফঘজ্যুয়হতগ্রফেদ
Create DataBindings
6
Greatest Common Divisor of 2 or more numbers.
123
Preprocessor in C# (#define and conditional operatives)