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]; }
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]; }
In fact, is there a way to generalise generics covering both array
s and List<>
s in one go? Or is it know to be not possible?
Could the answer even (gasp) encompass Collection
s?
PS, I apologize for not explicitly mentioning this is in the Unity3D milieu. For example "Random.Range" is just a Unity call (which does the obvious), and "AnyOne" is a completely typical extension or call in game programming.
Obviously, the question of course applies in any c# milieu.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
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]; }
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>
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.
2020, quick for Unity3D programmers: of course, nowadays modern versions of .Net are available in Unity!
T[]
and List<T>
actually both implement IList<T>
, which provides enumeration, a Count property and an indexer.
public static T AnyOne<T>(this IList<T> ra) { int k = ra.Count; int r = Random.Range(0,k); return ra[r]; }
Historical note: in past decades, this was the correct and only solution for Unity3D specifically, as in the Olden Days modern .Net was not available in Unity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With