Adjust the ref Parameter lab and replace ref with out wherever used. Verify that
ID: 3873531 • Letter: A
Question
Adjust the ref Parameter lab and replace ref with out wherever used.
Verify that the compiler is not happy with your method.
Fix your method by adding an else case that sets the parameter to 0.
In main, remove the initialization of your integer variable and verify we can still pass as an out parameter to our method.
Test as was tested using the same code.
using System.IO;
using System;
class Program
{
static void Main()
{
int n = 5;
changeNumber(ref n);
Console.WriteLine("Number: "+n);
changeNumber(ref n);
Console.WriteLine("Number: "+n);
}
static void changeNumber(ref int n) {
Console.WriteLine("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());
if(num > 10) {
n = num;
}
}
}
c#
Explanation / Answer
using System.IO;
using System;
class Program
{
static void Main()
{
int n = 5;
changeNumber(out n);
Console.WriteLine("Number: "+n);
changeNumber(out n);
Console.WriteLine("Number: "+n);
}
static void changeNumber(out int n) {
Console.WriteLine("Enter a number: ");
num = Convert.ToInt32(Console.ReadLine());
if(num > 10) {
n = num;
}else{
n = 0;
}
}
}
using System.IO;
using System;
class Program
{
static void Main()
{
int n = 5;
changeNumber(out n);
Console.WriteLine("Number: "+n);
changeNumber(out n);
Console.WriteLine("Number: "+n);
}
static void changeNumber(out int n) {
Console.WriteLine("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());
if(num > 10) {
n = num;
}else{
n = 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.