C#泛型,涵盖数组和列表? [英] c# generic, covering both arrays and lists?

查看:141
本文介绍了C#泛型,涵盖数组和列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个非常方便的扩展,这适用于任何事物的阵列

Here's a very handy extension, which works for an array of anything:

public static T AnyOne<T>(this T[] ra) where T:class
{
    int k = ra.Length;
    int r = Random.Range(0,k);
    return ra[r];
}



不幸的是它不能用于列表与LT工作;> ; 的东西。下面是任何列表与LT的工作原理相同的扩展;>

Unfortunately it does not work for a List<> of anything. Here's the same extension that works for any List<>

public static T AnyOne<T>(this List<T> listy) where T:class
{
    int k = listy.Count;
    int r = Random.Range(0,k);
    return listy[r];
}



其实,有没有办法来推广仿制药涵盖阵列列表与LT;> S IN一气呵成?或者是知道是不可能的?

In fact, is there a way to generalise generics covering both arrays and List<>s in one go? Or is it know to be not possible?

这发生在我,可以在回答进一步包括收藏 S'或者甚至低于已经专家实现了一个?

It occurs to me, could the answer even further encompass Collections? Or indeed has one of the experts below already achieved that??

PS,我不明确提到为此道歉是在Unity3D环境。 Random.Range是一个团结到了骨的功能,和一个人的呼叫内容100%的游戏引擎,任何游戏工程师。这是你在任何游戏中键入项目的第一个扩展,你用它不断地在游戏代码(任何爆炸!任何硬币音效!等等等等!)

PS, I apologize for not explicitly mentioning this is in the Unity3D milieu. "Random.Range" is a unity-to-the-bone function, and an "AnyOne" call reads as 100% game engine to any game engineers. It's the first extension you type in for any game project and you use it constantly in game code ("any explosion!" "any coin sound effect!" etc etc!)

显然,它当然可以在任何C#环境中使用。

Obviously, it could of course be used in any c# milieu.

推荐答案

在事实上之间最合适的通用接口 T [] 列表< T> 是 IReadOnlyList< T>

In fact the most appropriate common interface between T[] and List<T> for your case is IReadOnlyList<T>

public static T AnyOne<T>(this IReadOnlyList<T> list) where T:class
{
    int k = list.Count;
    int r = Random.Range(0,k);
    return list[r];
}



作为另一个答复中提到,的IList< T> 也可以,但是良好的实践要求你从主叫方请求的最小的方法,在这种情况下是计数功能code>属性和只读索引。

As mentioned in another answer, IList<T> also works, but the good practice requires you to request from the caller the minimum functionality needed by the method, which in this case is Count property and read only indexer.

的IEnumerable< T> 也能工作,但它允许调用者传递一个非集合迭代器,其中计数的ElementAt 扩展方法可能是非常低效的 - 像 Enumerable.Range(0,1000000),数据库查询等。

IEnumerable<T> also works, but it allows the caller to pass a non collection iterator where Count and ElementAt extension methods could be highly inefficient - like Enumerable.Range(0, 1000000), database query etc.

请注意:如果你看一下的最底部 IReadOnlyList接口文档,它是可用因为.NET 4.5。在早期版本的.Net你不得不求助于的IList< T> (自2.0可用)。团结停留远远落后于.Net的版本。对于2016年统一仅使用。NET 2.0.5。因此,对于Unity3D,你必须使用的IList< T>

Note for Unity3D engineers: If you look at the very bottom of the IReadOnlyList Interface documentation‌​, it is available since .Net 4.5. In earlier versions of .Net you have to resort to IList<T> (available since 2.0). Unity stays well behind on .Net versions. For 2016 Unity is only using .Net 2.0.5. So for Unity3D, you have to use IList<T>.

这篇关于C#泛型,涵盖数组和列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆