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/