0

I have a list of delegates:

public List<Func<bool>> Requirements { get; set; };

Now when I try to initialize it with an existing method, it works perfectly:

Requirements = new List<Func<bool>>() { obj.IsValid };

But how can I create a new method in this initialization and pass it to the list? Pseudocode:

Requirements = new List<Func<bool>>() { bool() {return true} };
Jachuc
  • 27
  • 8
  • 1
    I'm not sure what `bool() {return true}` is, but it's not the syntax for an inline delegate. Use a lambda, such as `() => true`. – canton7 Dec 22 '21 at 17:14
  • 1
    It's close to C# 10's new syntax... `bool () => { return true; }` is valid. C# 10 allows you to specify the return type. – madreflection Dec 22 '21 at 17:15
  • Thanks canton7. `() => true` works perfectly. @madreflection `bool () => { return true; }` might be valid c# but won't work in this case. – Jachuc Dec 22 '21 at 17:19
  • 1
    It's identical to `() => true` except that it's more explicit on a couple things. The only reason it wouldn't work is if you're not using C# 10. I'm not suggesting that you use it, mind you... but your example was *so close* to that syntax, it seemed like that was what you wanted, so I pointed out how close it was to valid C# 10 syntax. – madreflection Dec 22 '21 at 17:21
  • Yup, my mistake. `bool () => { return true; }` works perfectly too, just tested it wit C# 10. Thanks for the reply. – Jachuc Dec 22 '21 at 17:27

0 Answers0