How to use expanded property in C#

Asked

Viewed 100 times

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;
          }
       }
    }

1 answer

5


You have to create the private field to work, you are creating a loop infinite, because the property is used Idade to manipulate property Idade, which will then force you to manipulate Idade and so it goes.

using System;

public class Program {
    public static void Main() {
        var obj = new MinhaClasse();
        obj.Idade = 20;
        Console.WriteLine(obj.Idade);
        obj.Idade = 10;
        
    }
}

public class MinhaClasse {
    private int idade;
    public int Idade {
        get => idade;
        set => idade = value < 18 ? throw new Exception("Proibido para menores!") : value;
    }
}

Behold working in the ideone. And in the .NET Fiddle. And in the Coding Ground. Also put on the Github for future reference.

Every time you throw a Exception is doing something wrong. They even consider that this class should be abstract so that no one can use it. Many people consider it bad to make an exception on a property, at least in most cases. This is control flow with exception.

  • Why in the first example I gave the code works and does not fall in the loop?

  • Because you’re not manipulating your own property, somewhere in that code shows you’re doing it?

  • Because of the Set.

  • I don’t understand....

  • Help me understand something, in the first example, it creates an attribute for the class with the same property name?

  • No, he creates a field with the name he wants.

  • Hence, from the second example, it no longer creates this field and the property behaves as a method, so I have to create a field myself. That’s it?

  • Yes, it is always one method, or two if you do both operations. https://answall.com/q/133924/101

  • For situations like the second example, there could be something like a "Return value" (or something like that) within the set. This way he wouldn’t loop in and know it was to set the value of the property. So I wouldn’t have to create a field called age just for that and code would be cleaner. So I asked the question, I thought C# had something like this to solve the problem.

  • No, the set is void. Both fall into loop if you use your own name, and nothing other than using another name can solve this, let alone a return.

  • But when I use only the public int Idade {get; set;} he alone understands that it is to return the value of the property and "set it". When I say Idade=value he could understand that I do not want an infinite loop but make the set of property.

  • 1

    Yes............

  • Who saw any problem in the answer please inform me to clean up. Or take the negative for no reason.

  • It wasn’t me, I gave up a positive to balance again. The answer was very helpful to me.

  • @Maniero the use of Dataannotations would no longer be appropriate to validate model logic?

  • 1

    @Depends on various things, has uses that are useless, because it needs a framework validation, there are cases it doesn’t solve, there are cases it doesn’t make sense to use it, after all it is a declarative form, and most of the code needs to be imperative, the declarative is good when it will use somewhere else and the framework specific needs to understand the semantics of it. Generally I’m not a fan

  • @Maniero I understood. I remembered of this answer, I think the AP can take a look and learn a little about validation ;D

  • Yes, there was created a framework :) I don’t like everything.

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.