gpt4 book ai didi

c# - 无法将运算符 '<' 应用于类型 T 和 T 的操作数

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

这是我的代码

public void BubbleSort<T>(T[] array) where T : IComparable<T>
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 1; j < array.Length; j++)
{
if (array[j] < array[j - 1])
{

}
}
}
}

在你开枪之前因为没有搜索而失望。我已经搜索过,SO 上的答案之一是使用无与伦比的界面来解决问题。

不幸的是,我不会因为这个错误而去任何地方。

最佳答案

您似乎在期待 IComparable<T>允许您使用不等式运算符的约束。 IComparableIComparable<T>对直接使用不等式运算符什么都不说。相反,他们所做的是提供一个 CompareTo()您可以用来模拟不等式运算符的方法:

public void BubbleSort<T>(T[] array) where T: IComparable<T>
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 1; j < array.Length; j++)
{
if (array[j].CompareTo(array[j-1]) < 0)
{

}
}
}
}

关于c# - 无法将运算符 '<' 应用于类型 T 和 T 的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895125/

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