Exemplos de C# 4 - 006 - Enum

using System;

namespace 
_001_003_006_Enum
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Cor corPreferida 
Cor.Vermelho;

            
Console.WriteLine("COR PREFERIDA: {0} - {1}"
                (
int)corPreferida, corPreferida.ToString());

            
Cor corPreta Cor.Preto;

            
Console.WriteLine("COR FEIA.....: {0} - {1}"
                (
int)corPreta, corPreta.ToString());

            
Console.WriteLine();

            
Carro carro = new Carro()
                {
                    marca 
"Chevrolet",
                    modelo 
"Vectra",
                    cor 
Cor.Verde,
                    combustivel 
Combustivel.Gasolina | Combustivel.Álcool
                }
;

            
Console.WriteLine("{0} {1} da cor {2} usa {3} como combustível",
                carro.marca, carro.modelo, carro.cor, 
                CombustivelToString(carro.combustivel))
;

            
Console.ReadKey();
        
}

        
private static String CombustivelToString(Combustivel c)
        {
            String retorno 
"";

            foreach 
(int item in System.Enum.GetValues(typeof(Combustivel)))
            {
                
if (((int)c & item) == item)
                    retorno +
(Combustivel)item + ", ";
            
}

            
return retorno.Substring(0, retorno.Length-2);
        
}
    }

    
enum Cor
    {
        Vermelho, Verde 
10, Azul, Preto
    }
    
enum Combustivel
    {
        Gasolina 
0x1,
        Álcool 
0x2,
        Querosene 
0x4,
        Diesel 
0x8,
        BioDiesel 
0x16
    }

    
class Carro
    {
        
public String marca;
        public 
String modelo;
        public 
Cor cor;
        public 
Combustivel combustivel;
    
}
}

Comentar

  Country flag

biuquote
  • Comentário
  • Pré-visualização
Loading