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, 2012 at 08:26 PM

andresp gravatar image

273 61 46 46

(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, 2012 at 08:56 PM

rutter gravatar image

15.8k 8 31

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


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

Jun 05, 2012 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



Answers and Comments



asked: Jun 04, 2012 at 08:26 PM

Seen: 1992 times

Last Updated: Jun 08, 2012 at 01:45 PM