-5

I have a class

public class myObject 
{
   public decimal Prop1 {get;set;}
   public decimal Prop2 {get;set;}
   public decimal Prop3 {get;set;}
}

how i compare if Porp1,Prop2 or Prop3 has different value?

var obj1 = new myObject();
obj1.Prop1 = 1;
obj1.Prop2 = 1;
obj1.Prop3 = 1;

my obj1 has all equals properties

My implmentation:

public bool Test()
{
   if(Prop1 == Prop2)
   {
       return true;
   }
   if(Prop1 == Prop3)
   {
       return true;
   }
   if(Prop2 == Prop1)
   {
       return true;
   }
  if(Prop2 == Prop3)
   {
       return true;
   }

}

1 Answers1

1

Your implementation has a few problems:

  1. if (Prop1 == Prop2) { return true; }

    This means that the method will return true if Prop1 equals Prop2 and nothing else is important.

  2. if (Prop2 == Prop1)

    This does exactly the same as the one above and, thus, redundant. Checking if Prop1 equals Prop2 is the same as checking if Prop2 equals Prop1; does that make sense?

  3. The method doesn't return false, ever! It actually doesn't return in all code paths and, therefore, won't even compile.

When working on any programming problem, you should ask yourself how would you solve it in real life (or if you do it manually).

So, how do you know that three objects are equal (in your mind)? You basically compare two objects together and then compare the third one to either of them. So, in our example, you would think somehow like this:

Does Prop2 equal Prop1 the same? Okay, and does Prop3 also equal Prop1? If so, then the three are the same.

Now, how do we write this in code? In C#, the logical AND operator is && which we use to combine two conditions together. So, we write something like this:

if (Prop1 == Prop2 && Prop2 == Prop3) { /* All three are equal */ }

Or:

if (Prop1 == Prop2 && Prop1 == Prop3) { /* All three are equal */ }

Full example:

public class myObject 
{
    public decimal Prop1 {get;set;}
    public decimal Prop2 {get;set;}
    public decimal Prop3 {get;set;}

    public bool AreTheyEqual()
    {
        if (Prop1 == Prop2 && Prop2 == Prop3)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Test:

public static void Main(string[] args)
{
    var obj1 = new myObject();
    obj1.Prop1 = 1;
    obj1.Prop2 = 1;
    obj1.Prop3 = 1;
    Console.WriteLine(obj1.AreTheyEqual());    // True;
    obj1.Prop3 = 5;
    Console.WriteLine(obj1.AreTheyEqual());    // False;
}
  • I would suggest `return (Prop1 == Prop2 && Prop2 == Prop3);` is preferable to the `if` and then you can use `=>`. In general, I think you are likely doing something wrong if you do `return true` or `return false` and aren't in a loop. – NetMage Jul 03 '19 at 21:17
  • @NetMage I agree with the first part of your comment. However, the OP seems to be an absolute beginner and not familiar with the very basic stuff (check the comments above). So, I tried to simplify the logic as much as possible. I even thought about assigning `Prop1 == Prop2` and `Prop2 == Prop3` to two variables to make the logic more clear. – 41686d6564 stands w. Palestine Jul 03 '19 at 21:26
  • Regarding the second half, that's not necessarily true. There are cases where using `return true/false` (outside of a loop) is more appropriate in terms of readability. And even in cases where that's not the best way, I wouldn't go as far as calling it "wrong" as there's nothing **technically** wrong with doing so (at least AFAIK). – 41686d6564 stands w. Palestine Jul 03 '19 at 21:29
  • Well, I (almost always) think it is verbose and hence less readable, but YMMV. I also don't like simplifying code for beginners when it isn't how real code will be written. – NetMage Jul 03 '19 at 21:30
  • @NetMage [Verbose doesn't necessarily mean less readable and short code isn't always easier to read](https://softwareengineering.stackexchange.com/a/203686/228458). Moreover, it's not just readability; there are cases where using `return true`/`return false` isn't a poor choice. Consider `If (someCondition) { return true; } else { DoSomething(); return false }` (yes, you can assign the value to a bool variable and return at the end of the method but if the method is rather simple, I don't see anything wrong with two `return` statements. – 41686d6564 stands w. Palestine Jul 03 '19 at 21:41
  • I think `var cond = someCondition; if (!cond) DoSomething(); return cond;` is **much** better. – NetMage Jul 03 '19 at 21:47
  • In 1000 properties I need 1000 if? :( How check using reflect? Check multiple properties – Marcelo Dias Jul 04 '19 at 17:49
  • @MarceloDias I'm a little confused! You keep complaining about having to use multiple `if` statements although it was explained to you several times (in the comments and in this answer) that you do _not_ have to use multiple `if`s. – 41686d6564 stands w. Palestine Jul 04 '19 at 17:54