类<类型>在 C# 中

发布于 2024-11-07 19:49:42 字数 287 浏览 0 评论 0原文

我有一个类,希望将其作为列表使用:例如 List、List、...、List 我有一个 Randomizor 类,它将采用将被洗牌的集合数据类型。我怎样才能这样做呢?

class Randomizor<T>
{
    public Randomizor()
    {

    }

    public Array Shuffle(Array toShuffle)
    {

    }
}

I have a class and want to work with it as Lists: e.g. List<int>, List<string>, ... , List<T>
I have a class Randomizor which will take the collection data type that will be shuffled. How can I do so?

class Randomizor<T>
{
    public Randomizor()
    {

    }

    public Array Shuffle(Array toShuffle)
    {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离线来电— 2024-11-14 19:49:42

创建一个通用类,如下所示:

class Randomizer<TList, TType> where TList : IList<TType>
{
    public TList Randomize(TList list)
    {
        // ...
    }
}

或者像这样:

class Randomizer<T>
{
    public IList<T> Randomize(IList<T> list)
    {
        // ...
    }
}

Create a generic class like so:

class Randomizer<TList, TType> where TList : IList<TType>
{
    public TList Randomize(TList list)
    {
        // ...
    }
}

Or like so:

class Randomizer<T>
{
    public IList<T> Randomize(IList<T> list)
    {
        // ...
    }
}
凶凌 2024-11-14 19:49:42

不是很清楚的问题...你的意思是这样的吗?

public static class Randomizer<T>
{
   public static T GetRandom(List<T> list)
   {
      T value = default(T);
      // Perform some random logic.

      return value;
   }
}

Not very clear question... do you mean something like this?

public static class Randomizer<T>
{
   public static T GetRandom(List<T> list)
   {
      T value = default(T);
      // Perform some random logic.

      return value;
   }
}
柠北森屋 2024-11-14 19:49:42

编辑:经过一番挖掘后,我发现了两个高级实现,因此我建议优先使用它们。

用于此目的的扩展方法,之前已经建议过这里
我在下面包含了转述为 Shuffle 的代码。

public static IEnumerable<T> Shuffle<T> (this IEnumerable<T> source)
{     
    Random random = new Random ();     
    T [] copy = source.ToArray ();      
    for (int i = copy.Length - 1; i >= 0; i--) 
    {         
        int index = random.Next (i + 1);
        yield return copy [index];
        copy [index] = copy [i];
    }
}

一个有趣的解决方案改编自此 linq方法

public static IEnumerable<T> Shuffle<T> (this IEnumerable<T> source)
{     
    Random random = new Random ();     
    return source.OrderBy(i => Random.Next()).AsEnumerable();
}

原始答案但比编辑慢

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> sequence) 
{     
    Random random = new Random();
    List<T> copy = sequence.ToList();
    while (copy.Count > 0)
    {
        int index = random.Next(copy.Count);
        yield return copy[index];
        copy.RemoveAt(index);
    }
} 

如果您喜欢其中之一,您应该对链接的答案进行投票。

如果您非常关心随机性,可以升级到 RNG 来自加密 API 的算法,并为其注入一些不确定的值,例如从最近的鼠标活动生成的值。我怀疑这太过分了,而且会降低性能。

EDIT: I found two superior impementations after a little digging so I would suggest those in preference.

An extension method for this purpose and already been suggested previously here
I include the code paraphrased to Shuffle below.

public static IEnumerable<T> Shuffle<T> (this IEnumerable<T> source)
{     
    Random random = new Random ();     
    T [] copy = source.ToArray ();      
    for (int i = copy.Length - 1; i >= 0; i--) 
    {         
        int index = random.Next (i + 1);
        yield return copy [index];
        copy [index] = copy [i];
    }
}

And an interesting solution adapted from this linq approach

public static IEnumerable<T> Shuffle<T> (this IEnumerable<T> source)
{     
    Random random = new Random ();     
    return source.OrderBy(i => Random.Next()).AsEnumerable();
}

The orignal answer but slower than the edits

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> sequence) 
{     
    Random random = new Random();
    List<T> copy = sequence.ToList();
    while (copy.Count > 0)
    {
        int index = random.Next(copy.Count);
        yield return copy[index];
        copy.RemoveAt(index);
    }
} 

If you like one of these you should up vote the linked answer.

If you are very concerned about randomness, you could upgrade to one of the RNG algorithms from the Crypto API and seed it with some non deterministic value, like somthing generated from recent mouse activity. I suspect that would be overkill and it would degrade performance.

只是我以为 2024-11-14 19:49:42
class Randomizor<T>
{
    public Randomizor()
    {

    }

    public List<T> Shuffle(List<T> toShuffle)
    {

    }
}
class Randomizor<T>
{
    public Randomizor()
    {

    }

    public List<T> Shuffle(List<T> toShuffle)
    {

    }
}
别把无礼当个性 2024-11-14 19:49:42
class Randomizer<T>
{
    public Randomizer(ICollection<T> collection)
    {
        //Do something with collection using T as the type of the elements
    }            
}

但是,您可能想要使用通用扩展方法

static class Randomizer
{
    public static void Randomize<T>(this ICollection<T> collection)
    {
        //randomize the collection
    }
}

和用法:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
list.Randomize();
class Randomizer<T>
{
    public Randomizer(ICollection<T> collection)
    {
        //Do something with collection using T as the type of the elements
    }            
}

However you may want to go for a generic extension method

static class Randomizer
{
    public static void Randomize<T>(this ICollection<T> collection)
    {
        //randomize the collection
    }
}

and the usage:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
list.Randomize();
随梦而飞# 2024-11-14 19:49:42

也许像这样:

    public List<T> Shuffle<T>(List<T> toShuffle)
    {
        return toShuffle.OrderBy(x => Guid.NewGuid()).ToList();
    }

或者作为扩展方法

public static class Extensions
{
    public static List<T> Shuffle<T>(this List<T> toShuffle)
    {
        return toShuffle.OrderBy(x => Guid.NewGuid()).ToList();
    }
}

Maybe like this:

    public List<T> Shuffle<T>(List<T> toShuffle)
    {
        return toShuffle.OrderBy(x => Guid.NewGuid()).ToList();
    }

Or as an extension method

public static class Extensions
{
    public static List<T> Shuffle<T>(this List<T> toShuffle)
    {
        return toShuffle.OrderBy(x => Guid.NewGuid()).ToList();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文