Advertisement
NelIfandieva

Swapping Integer Values

Jul 28th, 2021
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main()
  4.         {
  5.  
  6.             int[] operatorValues = GetOperatorValues();
  7.  
  8.             var myClass = new Operator(operatorValues[0], operatorValues[1]);
  9.             var swapResult = myClass.SwapValues();
  10.             Console.WriteLine($"{swapResult.a} {swapResult.b}");
  11.         }
  12.  
  13.         private static int[] GetOperatorValues()
  14.         {
  15.             int counter = 0;
  16.             bool isNum;
  17.  
  18.             int[] result = new int[2];
  19.  
  20.             while (counter < 2)
  21.             {
  22.                 var input = Console.ReadLine();
  23.                 isNum = int.TryParse(input, out int num);
  24.  
  25.                 if (isNum)
  26.                 {
  27.                     result[counter++] = num;
  28.                 }
  29.             }
  30.  
  31.             return result;
  32.         }
  33.  
  34. public class Operator
  35.     {
  36.  
  37.         public Operator(int a, int b)
  38.         {
  39.             this.A = a;
  40.             this.B = b;
  41.         }
  42.  
  43.         public int A { get; private set; }
  44.    
  45.         public int B { get; private set; }
  46.  
  47.         public (int a, int b) SwapValues()
  48.         {
  49.             this.B += this.A;
  50.             this.A = this.B - this.A;
  51.             this.B -= this.A;
  52.  
  53.             return (this.A, this.B);
  54.         }
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement