5
I have a problem, I created a class containing the following property:
public class MinhaClasse
{
public int Idade {get; set;}
}
But when I do it:
public class MinhaClasse
{
   public int Idade {
      get{
         return Idade;
      }
      set{
         if (value < 18)
            throw new Exception("Proibido para menores!");
         else
            Idade = value;
      }
   }
}
The above code does not work and I am obliged to create a private attribute to store the value idade, or at least I have done so. Is that correct? Why can’t I use Idade = value?
public class MinhaClasse
    {
       private int _idade;
       public int Idade {
          get{
             return _idade;
          }
          set{
             if (value < 18)
                throw new Exception("Proibido para menores!");
             else
                _idade = value;
          }
       }
    }
tries public int age(int age)
– Brewerton Santos