Let’s set things right. What you want is to know if you have content in the object and not in the class. Class is just a model.
You can check if the object is null:
if (usuario == null)
Otherwise it depends on the object. It may be that the object has content is empty, but not null, but depends on the object semantics to know how to verify this.
For example, you say you have a class Usuario
with a property called Ativo
that is bool
can you verify:
if (usuario.Ativo)
If it is a array or a list (any enumerable) is possible to use the Count
you’re looking for:
if (lista.Count() > 0)
assuming you know that lista
is not null, otherwise it would be better:
if (lista != null && lista.Count > 0)
But it is almost certain that a Usuario
it’s not a list, and if it is, there’s something very wrong there.
If it is a string
may be that it works a check if it is different from empty:
if (texto != "")
or
if(!string.IsNullOrWhiteSpace(texto))
I put in the Github for future reference.
First you need to know what you’re dealing with and then find a solution.
Although the methods are different, in essence it is equal to Delphi. There is no universal solution.
Looking at her?
– Maniero
You want to know methods, properties ?
– Jorge Costa
Class is a prototype. You know nothing about it until it is instantiated into an Object. To understand why your question does not make sense, consider "class" as the floor plan of a house and you want to know if there is someone in the room and the house has not even been built yet...
– William John Adam Trindade
Sorry, I’m still a beginner, but I modified the question to see if it helps.
– Deivid Souza
You can check if the object was instantiated by checking if it is different from null
– Jorge Costa
usuario != null
????– jean
@Deividsouza you want to know if there is content in some object property?
– gato
Jean, Orge and Maniero answered my question, but I’m going to ask a question in the @Maniero answer because I tested it and it didn’t work as expected with the list. Thanks.
– Deivid Souza