When you create a classe
and a lista dessa classe
and tries to verify if a value coexists within that list there can be confusion in the method Contains that checks the instance of the class and not the actual value of the properties.
Example:
Class Dado
public class Dado
{
public string Nome { get; set; }
public int TransacaoBloqueio { get; set; }
}
Code 1:
IList<Dado> dadosBloqueioCompartilhado = new List<Dado>();
Dado dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;
if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
dadosBloqueioCompartilhado.Add(dadoAtual);
}
if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
dadosBloqueioCompartilhado.Add(dadoAtual);
}
In this code snippet the first if
will let you add the item inside the list, the second no, because the instance of classe
already exists within that list is the method Contains checks the object (the instance) and not its values.
Code 2:
IList<Dado> dadosBloqueioCompartilhado = new List<Dado>();
Dado dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;
if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
dadosBloqueioCompartilhado.Add(dadoAtual);
}
dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;
if (!dadosBloqueioCompartilhado.Contains(dadoAtual))
{
dadosBloqueioCompartilhado.Add(dadoAtual);
}
In this specific case you will get an unwanted surprise, because, the list will have added the two items, because, its instances are different and the Contains then checks the instances and not the values.
Code 3
Apparent solution for verifying the continuous data in the class:
Create a class by implementing the interface Iequalitycomparer:
public class ComparerDados : IEqualityComparer<Dado>
{
public bool Equals(Dado x, Dado y)
{
return (x.Nome.Equals(y.Nome)) &&
(x.TransacaoBloqueio.Equals(y.TransacaoBloqueio));
}
public int GetHashCode(Dado obj)
{
return 0;
}
}
And with the code below it will check the data contained in the class instance with the method Contains, by checking the values contained in the class instance:
IList<Dado> dadosBloqueioCompartilhado = new List<Dado>();
Dado dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;
if (!dadosBloqueioCompartilhado.Contains(dadoAtual, new ComparerDados()))
{
dadosBloqueioCompartilhado.Add(dadoAtual);
}
dadoAtual = new Dado();
dadoAtual.Nome = "A";
dadoAtual.TransacaoBloqueio = 1;
if (!dadosBloqueioCompartilhado.Contains(dadoAtual, new ComparerDados()))
{
dadosBloqueioCompartilhado.Add(dadoAtual);
}
Then you’ll only have one item on the list.
Observing: Nothing prevents you from using setNome
, but the ideal and standard of the architecture . NET is public string Nome {get ;set; }
Besides being more readable is the standard for any and all code. Example if do it the way setNome
in the ORM Entity Framework
he will not consider.
Put the given class and dataBlock Share too please! Already anti hand help so it doesn’t really work because the classes are different by the instances created! You will have to do otherwise! because you have to test the value! but, put the two classes I’m formulating an answer
– user46523
@João Colocar a classe Dado. The dataBloqueioCompartilh is a Data List.
– Mathi901
I’ve already worked out the answer. I’ll put a note on your class, it seems to use Java to set and get as property in the NET world is not like this!
– user46523