Advertisement
sandrovieira

Estudando Genéricos

May 28th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Trabalhando_com_Genericos_28._05._2015
  8. {
  9.     class Program
  10.     {
  11.         class VetorGenérico<T> where T : IComparable
  12.         {
  13.             private T[] Vetor;
  14.  
  15.             public VetorGenérico()
  16.             {
  17.                 Vetor = new T[10];
  18.             }
  19.  
  20.             public void EntraElemento(int Pos, T Elemento)
  21.             {
  22.                 Vetor[Pos] = Elemento;
  23.             }
  24.  
  25.             public T AchaMaior()
  26.             {
  27.  
  28.                 T Maior = Vetor[0];
  29.  
  30.                 for (int i = 1; i < 10; i++)
  31.                 {
  32.                     if (Vetor[i].CompareTo(Maior)>0)
  33.                     {
  34.                         Maior = Vetor[i];
  35.                     }
  36.  
  37.                 }
  38.  
  39.                 return Maior;
  40.             }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.         }
  47.         static void Main(string[] args)
  48.         {
  49.             VetorGenérico<int> MeuVetorInt = new VetorGenérico<int>();
  50.  
  51.             MeuVetorInt.EntraElemento(0, 34);
  52.             MeuVetorInt.EntraElemento(1, 43);
  53.             MeuVetorInt.EntraElemento(2, 12);
  54.             MeuVetorInt.EntraElemento(3, 55);
  55.             MeuVetorInt.EntraElemento(4, 31);
  56.             MeuVetorInt.EntraElemento(5, 40);
  57.             MeuVetorInt.EntraElemento(6, 76);
  58.             MeuVetorInt.EntraElemento(7, 55);
  59.             MeuVetorInt.EntraElemento(8, 22);
  60.             MeuVetorInt.EntraElemento(9, 15);
  61.  
  62.             Console.WriteLine("Maior Número: " + MeuVetorInt.AchaMaior().ToString());
  63.             Console.ReadKey();
  64.            
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement