c# - 可以 (a==1 && a==2 && a==3) 在没有多线程的情况下在 C# 中评估为 true 吗?

标签 c#

I know it could be done in JavaScript

但是否有任何可能的解决方案来在 C# 中根据下面给出的条件打印“Hurraa”而不使用多线程?

if (a==1 && a==2 && a==3) {
    Console.WriteLine("Hurraa");
}

最佳答案

当然,你可以重载operator ==来做你想做的事。

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();
        }
    }
}

关于c# - 可以 (a==1 && a==2 && a==3) 在没有多线程的情况下在 C# 中评估为 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48425981/

相关文章:

c# - 如何查找已安装的sql server 2014实例名称和版本?

c# - Tablet WPF Windows 桌面应用程序 - 滚动问题

c# - 以已知类作为分组键的 GroupBy

c# - 如何为打印报告编写一个简单的设计器

C# : start up an application on specific time

c# - 从 C# 中的字符串调用函数

c# - 我如何更新 EF CF 中的实体对象(分离)?

c# - 传递带有已指定参数的方法调用

c# - 为什么我的应用程序随着时间的推移变得响应速度变慢?

c# - String.Empty 不是 Const 有什么具体原因吗?