x


C# generics: using class generic in where clause of method generic

Why isn't this supported in Unity?

public class Container<T>
{
    private static class PerType<U> where U : T
    {
        public static U item;
    }

    public U Get<U>() where U : T
    {
        return PerType<U>.item;
    }

    public void Set<U>(U newItem) where U : T
    {
        PerType<U>.item = newItem;
    }
}

I'm getting this error:

error CS0305: Using the generic type Container.PerType' requires2' type argument(s)

more ▼

asked Jun 04 '12 at 08:26 PM

andresp gravatar image

andresp
258 21 24 25

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If you replace PerType<U> with Container<T>.PerType<U> on lines 10 and 15, it will at least compile. Could be that the compiler is having difficulty resolving a generic class that's nested within another generic class.

more ▼

answered Jun 04 '12 at 08:56 PM

rutter gravatar image

rutter
9k 4 16

thanks, it works. but that shouldn't be needed though:

http://stackoverflow.com/questions/10885260/c-sharp-generics-using-class-generic-in-where-clause-of-method-generic/1088536

it looks like it may have something to do with the Mono compiler then.

Jun 05 '12 at 09:05 AM andresp
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x7699
x231
x65

asked: Jun 04 '12 at 08:26 PM

Seen: 1615 times

Last Updated: Jun 08 '12 at 01:45 PM