- Home /
Compiler error CS0309 when using nested generics and System.Action
This code throws error CS0309 in Unity console, but works fine in .NET:
public class Bar { }
public class Foo<TBar>
where TBar : Bar
{
}
public class Test<TClass>
{
public void Run<TFoo, TBar, TSomething>(TSomething something)
where TFoo : Foo<TBar>
where TBar : Bar
{
Action del = delegate
{
var a = something;
};
}
}
This gives me the error:
error CS0309: The type `TFoo' must be convertible to `Foo<TFoo>' in order to use it as parameter `TFoo' in the generic type or method `Test<TClass>.<Run>c__AnonStorey0<TFoo,TBar,TSomething>'
I found the similar question on stackoverflow and it looks like that might be bug in old version of Mono that Unity is using. However, my code is slightly different from code in the question.
What is more interesting, that removing almost any piece from that code removes the error:
If you remove generic constraint from the Run() method, the compiler error is gone
If you remove "var a = something;" from delegate, the error is gone (!)
If you move "var a = something;" out of the delegate, the error is also gone (!!)
Finally, if you remove TClass generic type from Test class, the error is somehow gone (!!!)
Does anyone have any idea what might be going on here? Looks like I have a pretty unique combination of factors.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093314im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to shortcut a lambda expression through a function? 1 Answer
Strange Behavior with Dictionary of List of generic Actions 0 Answers
anonymous functions with JavaScript? 2 Answers
generics and c# in iPhone unity 2 Answers
How to create a generic list from a group of gameObjects with specific tag? 1 Answer