1

I wrote a class with the following ctor:

public class LambdaCondition<TSource> : BaseLambdaCondition
    where TSource : class, INotifyPropertyChanged
{
    // (...)

    public LambdaCondition(TSource source, Expression<Func<TSource, bool>> lambda, bool defaultValue)
    {
        // ...
    }
}

I then try to use it in the following way:

A a = new A();
var condition = new LambdaCondition(a, vm => vm.Prop, false);

Compiler however complains, that I have to specify templated parameter explicitly:

A a = new A();
var condition = new LambdaCondition<A>(a, vm => vm.Prop, false);

Why isn't the compiler able to deduce template parameter, so that it doesn't have to be specified explicitly? How can i change the code, so that user of my class can take advantage of automated guessing of template parameters?

Spook
  • 25,318
  • 18
  • 90
  • 167
  • I've experienced this too. I've always assumed this is just a shortcoming of Roslyn – SimonC Mar 07 '23 at 10:18
  • Generics are *not* templates. Using the term "template" is unnecessarily misleading, as (e.g.) the C++ rules and mechanisms for templates are almost, but not completely, entirely unlike those of .NET generics. – Jeroen Mostert Mar 07 '23 at 12:24

0 Answers0