Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较C中泛型类类型的两个对象#_C#_Generics - Fatal编程技术网

C# 比较C中泛型类类型的两个对象#

C# 比较C中泛型类类型的两个对象#,c#,generics,C#,Generics,我有一个家庭作业,我必须通过两个对象的一个属性来比较它们,但是由于我在链表上使用泛型类型,我必须在其中存储对象,所以我无法真正达到对象的特定属性(我使用的是一个接口,所有的类都实现了该接口) 所以我有一个主要的接口,一些类(比如class1,class2)继承了接口属性,比如年龄,或者任何一种易于比较的数字。在Main()中,我刚刚创建了一个新的LinkedList,并创建了一些class1、class2对象。我使用SortedListadElement方法将它们放在有序列表中,但我仍然停留在它

我有一个家庭作业,我必须通过两个对象的一个属性来比较它们,但是由于我在链表上使用泛型类型,我必须在其中存储对象,所以我无法真正达到对象的特定属性(我使用的是一个接口,所有的类都实现了该接口) 所以我有一个主要的接口,一些类(比如class1,class2)继承了接口属性,比如年龄,或者任何一种易于比较的数字。在Main()中,我刚刚创建了一个新的LinkedList,并创建了一些class1、class2对象。我使用SortedListadElement方法将它们放在有序列表中,但我仍然停留在它应该通过int属性比较元素的部分。我应该如何从那里继续使用这些源代码?我是否应该将第一行更改为:类列表:IEnumerable、IComparable、IMajorInterface?或者将比较中的某些内容更改为()?请帮忙

interface IMajorInterface 
{ 
    string Name{ get; } 
    int Age { get; set; } 
}

class List<T> : IEnumerable<T>, IComparable<T>
{
    ListElement<T> head;

    public List()
    {
        head = null;
    }

    public void SortedListAddElement(T newValue)
    {
        ListElement<T> new = new ListElement<T>(newValue);
        ListElement<T> p = head;
        ListElement<T> e = null;
        while (p != null /* && this is the part where I should compare the p and new objects by their property, for example the Age property, like: p.Age < new.Age */)
        {
            e = p;
            p = p.Next;
        }
        if (e == null)
        {
            new.Next = head;
            head = new;
        }
        else
        {
            new.Next = p;
            e.Next = new;
        }
    }

    public int CompareTo(T obj)
    {
        if (obj == null)
        {
            return -1;
        }
        else
        {
            if (obj is T)
            {
                T obj2 = obj as T;
                return obj.Age.CompareTo(obj2.Age);
            }
            else
            {
                return -1;
            }
        }
    }
interface-imajor接口
{ 
字符串名称{get;}
int Age{get;set;}
}
类列表:IEnumerable,IComparable
{
李斯特元素头;
公开名单()
{
head=null;
}
公共无效分类列表元素(T新值)
{
ListElement新建=新建ListElement(newValue);
ListElement p=头部;
liselement e=null;
while(p!=null/*&&这是我应该通过属性比较p和新对象的部分,例如Age属性,如:p.Age
c-sharpcorner上有一篇文章比较了泛型类型:

快速代码段:

 static bool Compare<T>(T Object1, T object2)
 {
      //Get the type of the object
      Type type = typeof(T);

      //return false if any of the object is false
      if (Object1 == null || object2 == null)
         return false;

     //Loop through each properties inside class and get values for the property from both the objects and compare
     foreach (System.Reflection.PropertyInfo property in type.GetProperties())
     {
          if (property.Name != "ExtensionData")
          {
              string Object1Value = string.Empty;
              string Object2Value = string.Empty;
              if (type.GetProperty(property.Name).GetValue(Object1, null) != null)
                    Object1Value = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
              if (type.GetProperty(property.Name).GetValue(object2, null) != null)
                    Object2Value = type.GetProperty(property.Name).GetValue(object2, null).ToString();
              if (Object1Value.Trim() != Object2Value.Trim())
              {
                  return false;
              }
          }
     }
     return true;
 }
静态布尔比较(T Object1,T object2)
{
//获取对象的类型
类型=类型(T);
//如果任何对象为false,则返回false
if(Object1==null | | object2==null)
返回false;
//循环遍历类中的每个属性,并从两个对象中获取属性值并进行比较
foreach(type.GetProperties()中的System.Reflection.PropertyInfo属性)
{
if(property.Name!=“ExtensionData”)
{
string Object1Value=string.Empty;
string Object2Value=string.Empty;
if(type.GetProperty(property.Name).GetValue(Object1,null)!=null)
Object1Value=type.GetProperty(property.Name).GetValue(Object1,null).ToString();
if(type.GetProperty(property.Name).GetValue(object2,null)!=null)
Object2Value=type.GetProperty(property.Name).GetValue(object2,null).ToString();
if(Object1Value.Trim()!=Object2Value.Trim())
{
返回false;
}
}
}
返回true;
}

您可以实现IComparer

class ageComparer : IComparer<Person>
   {
        public int Compare(Person x, Person y)
        {
            return x.Age - y.Age;
        }
   }
class-ageComparer:IComparer
{
公共整数比较(人员x、人员y)
{
返回x.年龄-y.年龄;
}
}

看看这篇文章

有几种方法可以解决这个问题。这里有一种方法。首先,让您的主要界面具有可比性:

interface IMajorInterface : IComparable<IMajorInterface>
{
    string Name{ get; }
    int Age { get; set; }
}
最后,作为另一种方法,您可以为其构造函数上的列表提供一个
IComparer
,就像
System.Collections.Generic
类一样。在上进行调查


我希望它能有所帮助。快乐编码!

你确定
列表
类应该实现
可比较
接口吗?按照
CompareTo
的实现方式,你可以将
obj
与自身进行比较(
obj2
obj
相同)。另外,请注意适当的术语:类不会从接口继承任何内容,它只能实现接口的成员。对不起,你是对的。我会更正这个。不,我绝对不确定。如果我使用泛型列表,我不知道我应该如何比较两个对象。我们的老师没有告诉我们关于排序林的任何信息ked列表,如果你有泛型类型,也没有关于比较的内容。我仍然不确定我是否正确地想象了你的赋值描述:你被告知你必须比较你自己的
T
类型的属性吗?那“主要接口”在哪里你指的是什么?你有没有提供任何代码?是的,我被告知我必须做比较部分。接口IMajorInterface{string Name{get;}int Age{get;set;}}我有几个类实现了上面的接口,所以属性也是。
class List<T> : IEnumerable<T>
    where T : IComparable<T>
{
    // ... Code omitted for brevity.
}
class List<T> : IEnumerable<T>
    where T : IMajorInterface // No need to implement IComparable<T> in any class.
{
    // ... Code omitted for brevity.
}