gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:42:47 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com