Advertisement
sandrovieira

Aula Indexadores

May 21st, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Aula_21._05._2015
  9. {
  10.     class Program
  11.     {
  12.         class Aluno
  13.         {
  14.             private int _matricula;
  15.             private string _nome;
  16.  
  17.             public int matricula
  18.             {
  19.                 get { return _matricula; }
  20.                 set {_matricula = value;}
  21.             }
  22.  
  23.             public string nome
  24.             {
  25.                 get { return _nome; }
  26.                 set {_nome = value;}
  27.             }            
  28.         }
  29.  
  30.         class Cadastro // INDEXADOR
  31.         {
  32.             private Aluno[] VetorAlunos = new Aluno[3];
  33.  
  34.             public Aluno this[int i]
  35.             {
  36.                 get { return VetorAlunos[i]; }
  37.                 set { VetorAlunos[i] = value; }
  38.             }
  39.         }
  40.  
  41.         static void Main(string[] args)
  42.         {
  43.             Cadastro MeuCadastro = new Cadastro();
  44.  
  45.             Aluno X;
  46.  
  47.             X = new Aluno();
  48.             X.matricula = 123;
  49.             X.nome = "Bernardo";
  50.  
  51.             MeuCadastro[0] = X;
  52.  
  53.             X = new Aluno();
  54.             X.matricula = 456;
  55.             X.nome = "Aline";
  56.  
  57.             MeuCadastro[1] = X;
  58.  
  59.             X = new Aluno();
  60.             X.matricula = 789;
  61.             X.nome = "Marta";
  62.  
  63.             MeuCadastro[2] = X;
  64.  
  65.             for (int i = 0; i < 3; i++)
  66.             {
  67.                 Console.WriteLine("{0} - {1}", MeuCadastro[i].matricula,MeuCadastro[i].nome);
  68.             }
  69.  
  70.             Console.ReadKey();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement