How do I know if there is content in an object of the class?

0

I need to know if I have content in a class or if it is empty, how do I get this information?

Classe usuario = new Classe();

I wanted to be able to see if there is content in this class of my usuario . In Delphi there is something like Count for example.

    
asked by anonymous 16.07.2018 / 22:16

1 answer

3

Let's set things right. You want to know if you have content in the object, not in the class. Class is just a template .

You can check if the object is null:

if (usuario == null)

Outside this depends on the object. It may be that the object has content is empty, but not null, but it depends on the object's semantics to know how to verify it.

For example, you say that you have a class Usuario with a property named Ativo that is bool you can check:

if (usuario.Ativo)

If it is an array or a list (any enumerable) you can use the Count you want:

if (lista.Count() > 0)

Assuming that lista is not null, otherwise it would be better:

if (lista != null && lista.Count > 0)

But it is almost certain that a Usuario is not a list, and if it is, there is something very wrong there.

If it is a string , a check may work if it is not empty:

if (texto != "")

or

if(!string.IsNullOrWhiteSpace(texto))

First you need to know what you're dealing with then find a solution.

Although the methods are different, it is essentially the same as Delphi. There is no universal solution.

    
16.07.2018 / 22:33