From: Rich P on
I have been seeing code samples using notation like this:

...
T[] data = source.ToArray();
...

When I try the sample in VS2008 -- I get the following error message:

"The type or namespace name 'T' could not be found (are you missing a
using directive or an assembly reference?"


Is the notation above a VS2010 thing? If yes, is there an equivalent
for VS2008? If no, what do I need to add so that VS2008 does not
complain about T[] something = ... ?

Thanks

Rich

*** Sent via Developersdex http://www.developersdex.com ***
From: Alberto Poblacion on
"Rich P" <rpng123(a)aol.com> wrote in message
news:e75Cw1WDLHA.4636(a)TK2MSFTNGP02.phx.gbl...
> ..
> T[] data = source.ToArray();
> ..
>
> When I try the sample in VS2008 -- I get the following error message:
>
> "The type or namespace name 'T' could not be found (are you missing a
> using directive or an assembly reference?"

This will only work if you use it inside a class or method that is
marked as "Generic" and contains the parameter T:

public void MyMethod<T> ()
{
..
T[] data = source.ToArray();
..
}

Note the <T> in the declaration of the method. That is what tells the
compiler to treat the T in T[] as a class name. The real class will be
passed to the method when it is inivoked:

MyMethod<string>();

The preceding causes the "T" to become "string" when caling the method.
You can do a similar thing at the class level by adding <T> after the
class name.

From: Rich P on
Thank you for your reply. This did fix one of the complaints from
VS2008, but he is still complaining about one other guy. Here is the
sample class that I copied from another post with the recommended fix of
adding <T> to ...class EnumerableExtensions :

public static class EnumerableExtensions<T>
{
private static readonly Random random = new Random();

//--VS2008 is complaining about "selecttrandomitem<t>"

public static ienumerable<t> selectrandomitem<t>(this ienumerable<t>
source, int total)
{
if (source == null) yield break;

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);

yield return data[index];

data[index] = data[data.Length - i - 1];
}
}
}

Here is the error message for "selecttrandomitem<t>"

"The body of
'MySampleApp.EnumerableExtensions<T>.selectrandomitem<t>(ienumerable<t>,
int)' cannot be an iterator block because 'ienumerable<t>' is not an
iterator interface type"

This sample class is great for learning Generics in C#. May I ask how I
could fix this error based on the context of the sample above?

Thanks

Rich

*** Sent via Developersdex http://www.developersdex.com ***
From: Alberto Poblacion on
"Rich P" <rpng123(a)aol.com> wrote in message
news:OXm3jsXDLHA.4400(a)TK2MSFTNGP05.phx.gbl...
> "The body of
> 'MySampleApp.EnumerableExtensions<T>.selectrandomitem<t>(ienumerable<t>,
> int)' cannot be an iterator block because 'ienumerable<t>' is not an
> iterator interface type"

I believe that it is an uppercase/lowercase problem. You probably want
IEnumerable rather than ienumerable. The former is an iterator, while the
latter will just throw a compilation error unless you have defined
ienumerable somewhere else in your code. Also, be careful with your <t>
parameters -- you are mixing upper and lower case, giving raise to two
different generic parameters, which is probably not what you want.

From: Matt on
On Jun 16, 11:41 am, Rich P <rpng...(a)aol.com> wrote:
> Thank you for your reply.  This did fix one of the complaints from
> VS2008,  but he is still complaining about one other guy.  Here is the
> sample class that I copied from another post with the recommended fix of
> adding <T> to ...class EnumerableExtensions :
>
> public static class EnumerableExtensions<T>
> {
>   private static readonly Random random = new Random();
>
>    //--VS2008 is complaining about "selecttrandomitem<t>"
>
>   public static ienumerable<t> selectrandomitem<t>(this ienumerable<t>

Yes, C# is case sensitive. This should be IEnumerable<T> and so forth.

Matt