SelectRandomItem is an extension method and it can only be declared on a
*non-generic* static class. You had mistakenly make the class generic by
adding<t> on the class definition. That's the source of error.
<
Thank you for your reply. I still get an error if I copy your class
like this:
public static class EnumerableExtensions
{
private static readonly Random random = new Random();
//--VS complains here about selectrandomitem
public static ienumerable<t> selectrandomitem<t>(this ienumerable<t>
source, int total)
{
if (source == null) yield break;
//--VS complains about T[]
T[] data = source.ToArray();
if (total< 0 || total> data.Length)
{
throw new ArgumentException();
}
for (int i = 0; i< total; i++)
{
int index = random.Next(data.Length - i);
//--and VS complains about data[index]
yield return data[index];
data[index] = data[data.Length - i - 1];
}
}
}
As I said previously, If you had copy the code exactly as it appear, it
should compile fine. You had mistakenly changed the casing for
Here are the error messages I am getting with this class (VS2008)
--error1
The body of
'ConsoleApplication1.EnumerableExtensions.selectrandomitem<t>(ienumerabl
e<t>, int)' cannot be an iterator block because 'ienumerable<t>' is not
an iterator interface type
As others has said, C# is *case-sensitive*. There is no ienumerable<t>
in the framework, only IEnumerable<T> which is defined in
System.Collections.Generic namespace. Since I had use 'yield break' and
'yield return' statement in the method block, the return type of the
SelectRandomItem method must be of type IEnumerable said:
--error2
The type or namespace name 'T' could not be found (are you missing a
using directive or an assembly reference?)
I had made the SelectRandomItem method generic by adding <T> at the name
of methods. You had mistakenly changed the casing from uppercase <T> to
lowercase <t>. Generic type declaration have to be consistent with the
usage inside the method. If you use lowercase 't' on the method
definition, you must also use the same lowercase 't' inside the method body.
--error3
Cannot implicitly convert type 'T' to 'object'
If you resolve the error2 above, this error should go away.
What should I do to eliminate these errors?
Well, read the code carefully and work up on the proper casing.
Also, not sure if this is of any significance, but I also see that a lot
of code sampels have
* list<something> x = new list<something>() *
on my workstation I don't have a * list *. I have to use uppercase *
List *. Is my VS2008 different? I am pretty sure I have all the latest
patches for it and service packs (version 3.5 SP1).
Care to share where do you get that code sample? Maybe a typing error or
maybe the author of that code did defined a new type named list<T>?
Without the original code, I cannot say for sure but as far as .NET is
concerned, all classes name in the framework starts with the uppercase
letter.
In order to aid your understanding of my code, below are the features
that I had used and the links to MSDN website for the details
1. Generics
http://msdn.microsoft.com/en-us/library/512aeb7t(VS.90).aspx
2. Iterators
http://msdn.microsoft.com/en-us/library/dscyy5s0(VS.90).aspx
3. Extension Methods
http://msdn.microsoft.com/en-us/library/bb383977(VS.90).aspx
Regards.