-2

So I'm working on a really simple lab for my college class and have run into a bit of a problem. My teacher failed to thoroughly explain when you would use ref and when to use out. The assignment was change a pre-written method to use ref, then make sure it ran, then change it to use out. I got the ref part down, but how do I rewrite the method to use out? Below is the program

using System;

static class Program
{
    /// <summary>
    /// Purpose: Entry point to your C# program
    /// </summary>
    static void Main()
    {
        int iVal1 = 5;
        int iVal2 = 7;
        //Call the Swap method with two arguments
        Swap(ref iVal1, ref iVal2);
        Console.WriteLine("Swapped values first {0:D} second {1:D}", iVal1, iVal2);
        Console.WriteLine("Press Enter to continue ...");
        Console.ReadLine();
    }//End Main()

    /// <summary>
    /// Purpose: To swap the two parameters passed to this method
    /// </summary>
    /// <param name="num1">num1 int, first number</param>
    /// <param name="num2">num2 int, second number</param>
    static public void Swap(ref int num1, ref int num2)
    {
        int tempInt = num1;
        num1 = num2;
        num2 = tempInt;
    }
}//End class Program
Brandon
  • 281
  • 2
  • 17

1 Answers1

2

out when you want to initialize the property's reference and ref when you may change a reference.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99