Advertisement
maroxcore

params 1

Jan 23rd, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1.         static void Main(string[] args)
  2.         {
  3.  
  4.             int suma = Sumar2(10,20,30,40,50, "1000");
  5.             Console.WriteLine(suma);
  6.             Console.ReadKey();
  7.         }
  8.  
  9.         static int Sumar(params object[] numeros)
  10.         {
  11.             int suma = 0;
  12.             foreach (var item in numeros)
  13.             {
  14.                 if (item is int)
  15.                 {
  16.                     suma += (int)item;
  17.                 }
  18.                 else if (item is string)
  19.                 {
  20.                     //int convertido2 = int.Parse((string)item);
  21.                     bool convertido = int.TryParse((string)item, out int temporal);
  22.                     if (convertido)
  23.                     {
  24.                         suma += temporal;
  25.                     }
  26.                     else
  27.                     {
  28.                         throw new Exception("Valor no numerico");
  29.                     }
  30.                 }
  31.                 else
  32.                 {
  33.                     throw new Exception("Valor no numerico");
  34.                 }
  35.             }
  36.             return suma;
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement