我正在尝试编写一个泛型方法,它应该支持ex int,double,float等的内部类型.该方法是对数组进行排序.我得到一个编译时错误,说"我不能应用运算符<到类型T",但我该如何解决呢?我应该使类通用并使用约束吗?这是我的代码:
public static T[] Sort<T>(T[] inputArray)
{
for (int i = 1; i < inputArray.Length; i++)
{
for (int j = i - 1; j >= 0; j--)
{
***if (inputArray[j + 1] < inputArray[j])***
{
T temp = inputArray[j + 1];
inputArray[j + 1] = inputArray[j];
inputArray[j] = temp;
}
else
{
break;
}
}
}
return inputArray;
}
Run Code Online (Sandbox Code Playgroud) 我需要创建泛型方法,返回两个参数中的更多.运营商>和<不起作用.这是我方法的签名:
public static T Greater<T>(ref T a, ref T b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
Run Code Online (Sandbox Code Playgroud)
我是C#中的新手,也是泛型类型的新手.
我试图将 AVL 树转换为通用树,但遇到了一些类型错误。
运算符不能应用于类型“T”和“T”的操作数
private Node RecursiveInsert(Node current, Node n)
{
if (current == null)
{
current = n;
return current;
}
else if (n.data < current.data) // Gives Error {
current.left = RecursiveInsert(current.left, n);
current = balance_tree(current);
}
else if (n.data > current.data) // Gives Error {
current.right = RecursiveInsert(current.right, n);
current = balance_tree(current);
}
return current;
}
Run Code Online (Sandbox Code Playgroud)
///
public class Node
{
public T data { get; set; }
public Node left { get; set; }
public Node …Run Code Online (Sandbox Code Playgroud) 我的问题是这样的: c#list compare
但唯一需要注意的是:
我正在使用.NET Framework 2.0
那么如何在C#framework 2上比较两个列表并在项目不同时返回一个布尔值呢?
instance == anotherone fails
instance.Equals(anotherone) fails.
Run Code Online (Sandbox Code Playgroud)
编辑:
他们都是List
编辑2
我正在尝试比较列表值是否正确.我可以对它们进行排序,np.问题是如果项目的计数或值发生变化.例如:
List1->Item1 = "A"
List1->Item2 = "B"
List2->Item1 = "B"
List2->Item2 = "A"
//must return true
List1->Item1 = "A"
List1->Item2 = "B"
List2->Item1 = "B"
List2->Item2 = "C"
//must return false
List1->Item1 = "A"
List1->Item2 = "B"
List2->Item1 = "B"
List2->Item2 = "A"
List2->Item3 = "A"
//must return false, etc.
Run Code Online (Sandbox Code Playgroud)
谢谢和亲切的问候.