-
Not sure if the question is suitable here. using System;
namespace ConsoleApp1
{
class Program
{
static T GetInstance<T>()
where T : IDisposable, new()
{
return new T();
}
static void DoSomethingOverload1<T>()
where T : IDisposable, new()
{
// Error CS0407
// 'T Program.GetInstance<T>()' has the wrong return type
DoSomething(GetInstance<T>);
}
static void DoSomethingOverload2<T>()
where T : IDisposable, new()
{
// No error
DoSomething(() => new T());
}
static void DoSomething(Func<IDisposable> factory)
{
// TODO
}
}
} When Thanks for reading the question. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
static void DoSomethingOverload1<T>()
where T : class, IDisposable, new()
{
// No error
DoSomething(GetInstance<T>);
} Delegate variance requires a reference type. So you need |
Beta Was this translation helpful? Give feedback.
-
@rvhuang, more of a StackOverflow question. Proposal to have this thread closed. Although insightful it doesn't address issues within the language. |
Beta Was this translation helpful? Give feedback.
-
@rvhuang in addition to what @ethanae said, if you're a bit intimidated by SO or don't use it then you can also create a gist for the code and then ask questions at C# channel, alternatively, join the community that fits your needs, check it out here. |
Beta Was this translation helpful? Give feedback.
Delegate variance requires a reference type. So you need
class
constraint.