7

I know it could be done in JavaScript

But is there any possible solution to print "Hurraa" on the condition given below in C# without multi-threading?

if (a==1 && a==2 && a==3) {
    Console.WriteLine("Hurraa");
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
zsubzwary
  • 1,196
  • 1
  • 14
  • 22

4 Answers4

22

Sure, you can overload operator == to do whatever you want.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var a = new AlwaysEqual();
            Assert.IsTrue(a == 1 && a == 2 && a == 3);
        }

        class AlwaysEqual
        {
            public static bool operator ==(AlwaysEqual c, int i) => true;
            public static bool operator !=(AlwaysEqual c, int i) => !(c == i);
            public override bool Equals(object o) => true;
            public override int GetHashCode() => true.GetHashCode();
        }
    }
}
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
  • 3
    It is so easy in C# compared to JavaScript. – Salman A Jan 24 '18 at 15:36
  • @SalmanA Yeah, I'm always kind of surprised that JavaScript lets you change the behavior of `toString` and `valueOf` and properties of `window`, but doesn't have any concept of operator overloading. – Jacob Krall Jan 24 '18 at 15:38
  • @JacobKrall, ^^ not yet. – Nina Scholz Jan 24 '18 at 15:39
  • This is a lot more lines compared to JS – carkod Jan 25 '18 at 09:23
  • @carkod: How so? `class AlwaysEqual` has five lines of code (two of them unnecessary); `const a = {` on the accepted JavaScript answer (https://stackoverflow.com/a/48270314/3140) is three lines of code. Everything else is testing to prove that it works. – Jacob Krall Jan 25 '18 at 17:24
20

Sure, its the same concept as a few of the javascript answers. You have a side effect in a property getter.

private static int _a;
public static int a { get { return ++_a; } set { _a = value; } }
static void Main(string[] args)
{
    a = 0;
    if (a == 1 && a == 2 && a == 3)
    {
        Console.WriteLine("Hurraa");
    }
    Console.ReadLine();
}
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • 2
    Of course you do not really need the assignment `a = 0`, and you can leave out the `set` accessor inside `a` entirely. Just if you want a shorter code example. – Jeppe Stig Nielsen Jan 26 '18 at 18:09
3

It depends on what is a. We could create a class so it's instance would behave like shown above. What we have to do is to overload operators '==' and '!='.

    class StrangeInt
    {
        public static bool operator ==(StrangeInt obj1, int obj2)
        {
            return true;
        }

        public static bool operator !=(StrangeInt obj1, int obj2)
        {
            return false;
        }
    }


    static void Main(string[] args)
    {
        StrangeInt a = new StrangeInt();
        if(a==1 && a==2 && a==3)
        {
            Console.WriteLine("Hurraa");
        }
    }
Alex
  • 790
  • 7
  • 20
3

C# with Property

static int a = 1;
static int index
{
    get
    {                
        return (a++);
    }
}

static void Main(string[] args)
{
    if (index == 1 && index == 2 && index == 3)
        Console.WriteLine("Hurraa");
}
programtreasures
  • 4,250
  • 1
  • 10
  • 29