【问题标题】:How to use a variable from arbitrary type class?如何使用任意类型类的变量?
【发布时间】:2022-01-21 03:02:21
【问题描述】:

我有一个方法,它采用任意类型的数组。我有一个对象数组(在我的例子中,这是一个带变量的类)。然后我把我的对象数组放在这个方法中。那么如何使用这个对象中的变量呢?

public class C           // This one of my classes
{
    public int I { get; set; }
}

public static void Sort<T>(T[] array, string name) // Here i put my class as argument
{
    ...
    Array.Sort<T>(array, (a, b) => a.I.CompareTo(b.I)); // Here "I" is some variable in my class, which i need to use
    ...
}

static void Main(string[] args) // Here i create an array of classes
{
    ...
    C[] classes = new C[100000];
    Sort(classes);
    ...
}

【问题讨论】:

  • 如果您传入string[] 或任何其他类型的数组,您希望它如何工作?如果您只需要 Sort 方法与 C[] 一起使用,请不要使其通用...
  • 他们都可以实现一个暴露该 int 的接口吗?然后你可以有一个IHaveAnInt 实例数组
  • 是的,一个接口作为去这里的方式。
  • 或在类上实现 IComparable 并具有类型约束,例如 where T:IComparable 然后您可以执行 a.CompareTo(b) 并且“I”成员成为 IComparable 的实现细节.实际上,如果该类实现 IComparable 您可能只需执行 .Sort() 就可以了
  • Array.Sort 方法可以将IComparer&lt;T&gt; 作为参数。不要让你的类型实现IComparable,而是创建一个知道如何比较你的类型的第二种类型。创建具有 int 属性 getter 的 IHaveAnInt 接口(我在上面制作的)。让您的所有类型都实现该接口。创建一个实现IComparer&lt;IHaveAnInt&gt; 的类型,该类型了解如何比较两个 IHaveAnInts。将该类型的实例传递给Array.Sort

标签: c# arrays class


【解决方案1】:

我建议你在你的类中实现IComparable<T>接口,你甚至不需要专门的排序方法

请考虑以下示例代码

using System;
using System.Collections.Generic;

namespace Test
{
    class Comparable : IComparable<Comparable>
    {
        public int Number { get; set; }

        public int CompareTo(Comparable other)
        {
            if (other == null)
                return 1;

            return Number.CompareTo(other.Number);
        }

    }

    class Program
    {
        static void Main()
        {
            List<Comparable> comparableList = new List<Comparable>
            {
                new Comparable { Number = 75 },
                new Comparable { Number = 1 },
                new Comparable { Number = 23 }
            };

            comparableList.Sort();

            foreach (Comparable comparable in comparableList)
                Console.WriteLine(comparable.Number);
        }

    }
}

编辑

如果您在评论中指出必须使用Array.Sort() 方法,那么您可以轻松地将列表转换为数组并这样做

Comparable[] comparableArray = comparableList.ToArray();
Array.Sort(comparableArray);
foreach (Comparable comparable in comparableArray)
    Console.WriteLine(comparable.Number);

【讨论】:

  • 感谢您的帮助,但根据我的任务,我必须使用 Array.Sort() 方法
  • 您可以轻松地将列表转换为数组并这样做:请参阅我的答案的编辑
【解决方案2】:

这是你想要的吗?

public class C  
{
    public int I { get; set; }
}

public class CComparer : IComparer<C>
{
    public int Compare(C x, C y)
    {
        return x.I.CompareTo(y.I);
    }
}

static class Program
{
    static void Main(string[] args)
    {
        var array = new C[100];

        // Calls CComparer.Compare(x,y);
        Array.Sort(array, new CComparer());
    }
}

另一个版本是

public class C  
{
    public int I { get; set; }

    public static IComparer<C> Comparer { get; } = new CComparer();

    class CComparer : IComparer<C>
    {
        internal CComparer() { }
        public int Compare(C x, C y)
        {
            return x.I.CompareTo(y.I);
        }
    }
}

static class Program
{
    static void Main(string[] args)
    {
        var array = new C[100];

        Array.Sort(array, C.Comparer);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多