-1

What is the idea of c# Predicate ? Is it possible to rewrite complex condition check logic using predicates?

private bool HasChanged(object originalValue, object currentValue)
{
    if ((originalValue == null && currentValue != null) || 
        (originalValue != null && !originalValue.Equals(currentValue)))
    {
        return true;
    }
    
    return false;
}
Fildor
  • 14,510
  • 4
  • 35
  • 67
Mikhail Kostiuchenko
  • 9,121
  • 4
  • 15
  • 28
  • 1
    Does this answer your question? [Predicate Delegates in C#](https://stackoverflow.com/questions/556425/predicate-delegates-in-c-sharp) – gunr2171 Jul 14 '22 at 12:20
  • 1
    `Predicate` is essentially just a wrapper for `Func` and can be used to encapsulate code that takes a single value and returns a `bool` result. Take a look at [Delegates: Predicate vs. Action vs. Func](https://stackoverflow.com/questions/566860/delegates-predicate-vs-action-vs-func) for more info – phuzi Jul 14 '22 at 12:20
  • 1
    See also [What is a predicate in c#?](https://stackoverflow.com/questions/1710301/what-is-a-predicate-in-c) – gunr2171 Jul 14 '22 at 12:21
  • [Predicate](https://learn.microsoft.com/en-us/dotnet/api/system.predicate-1?view=netframework-2.0) predates, later more generic delegates like [Func](https://learn.microsoft.com/en-us/dotnet/api/system.func-1?view=netframework-3.5) which were introduced with LINQ. – Jodrell Jul 14 '22 at 12:30

4 Answers4

3

Predicate<T> is just a delegate that takes a T and returns bool. It's the equivalent of Func<T, bool> or a method with the signature bool Method<T>(T input).

It doesn't really help you here with your method.

You can, however, simplify your method to this:

private bool HasChanged(object originalValue, object currentValue) =>
    originalValue == null
    ? currentValue != null
    : !originalValue.Equals(currentValue);
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

Try this:

bool HasChanged(object originalValue, object currentValue)
    => !(originalValue?.Equals(currentValue) ?? currentValue == null);
Petr Voborník
  • 1,249
  • 1
  • 14
  • 11
0

You can use a predicate to check on object agains another object declaring the predicate as an object method. Something like

class MyClass
{
    public bool isValid(object obj)
    {
        if (obj is MyClass)
            return true;
        return false;
    }
}

public static class Program
{ 
    static int Main()
    {
        MyClass c1 = new ();

        Predicate<MyClass> predicate = c1.isValid;
        MyClass c2 = new ();
        if (predicate(c2))
        {
            Console.WriteLine("c1 is valid");
        }
        else 
        {
            Console.WriteLine("c1 is not valid");
        }

        return 0;
    }
}
Marco Beninca
  • 605
  • 4
  • 15
0

You could simplify this a little by flipping the logic and not attempting to cram it in to a single clause

private bool HasChanged(object originalValue, object currentValue)
{
    var bothNull = originalValue == null && currentValue == null;

    if (bothNull){
        return false;
    }

    // at least one of them is not null
    var areSame = originalValue?.Equals(currentValue) ?? false;

    return !areSame;
}
phuzi
  • 12,078
  • 3
  • 26
  • 50