Función que devuelve falso al evaluar [cerrado]
Frecuentes
Visto 107 veces
-3
Any idea why the func is returning false? Equals does not fire as well!!!
class Program
{
static void Main(string[] args)
{
Func<Person, SomethingElse, bool> matchNested =
(p, s) => p.Nested == s.Nested;
var matched = matchNested(new Person()
{
Age = 10,
Nested = new Nested()
{
Validity = DateTime.Today
}
},
new SomethingElse()
{
Age = 10,
Nested = new Nested()
{
Validity = DateTime.Today
}
});
Console.WriteLine(matched);
}
}
internal class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Nested Nested { get; set; }
}
internal class SomethingElse
{
public string Name { get; set; }
public int Age { get; set; }
public Nested Nested { get; set; }
}
internal class Nested
{
public DateTime Validity { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
if (!this.Validity.Equals((obj as Nested).Validity))
return false;
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
3 Respuestas
4
I'm not going to bother trying to figure out what your Equals
code is trying to do, I'm just saying why it isn't being called and why you are always getting false.
De forma predeterminada, ==
operator returns true for reference types where they point at the mismo objeto, de acuerdo con los documentos.
So what you could do is sobrecargar al operador en tu Nested
class, or call p.Nested.Equals(s.Nested)
. If you go the overload route, you will need to overload !=
también.
Si vas al Equals
route, you can have your Nested
clase implementar el Equatable<T>
interfaz. so you get a strongly-typed Equals
method instead of one taking object
.
Respondido el 12 de junio de 12 a las 09:06
But I'm not pointing at the same object!!! Each Nested has new Nested() instances. - Mike
@Mike Exactly... so you know you are not point at the same object and the documentation says how ==
will behave. That is why it constantly returns false for you. - adam houldsworth
0
If you want this to work you need to make sure you also override the == operator on Nested - you have only overridden Equals but you are calling Nested == Nested
Respondido el 12 de junio de 12 a las 09:06
0
It seems like you want two Nested types to be equal if their Validity properties are equal. If so, remove from Equals() the "return base.Equals(obj)" and change the line above it as follows:
return (this.Validity.Equals((obj as Nested).Validity));
Also, Equals() won't be called unless you also change your Func<> like so:
Func matchNested = (p, s) => p.Nested.Equals(s.Nested);
Respondido el 12 de junio de 12 a las 09:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# c#-4.0 or haz tu propia pregunta.
Por favor, aplique una sangría a su código. - Bali C
La
throw
is not commented ? - V4VendettaLa forma en que estás usando
DateTime.Today
is not good enough for production software. This is asking for elusive issues just around midnight. Call this property only once per the whole operation. - Jirka HanikaYou removed the function from the question... It's hard to answer something about code that you don't show... - Guffa