1

It seems it is possible to compare two delegates in C# for equality, but I cannot figure out if one can compare two variables of type Func<>

Func<Task<Foo>> a = () => _service.GetFoo();
Func<Task<Foo>> b = () => _service.GetFoo();

Is it possible to do some comparison of these?

P.S. I need this for unit testing, I do pass a delegate like that to another method and I'd like to test that this is what is being passed (not _service.GetBar() that is). I have now solved this by calling the delegate and comparing the result, but I'd rather compare it directly if it is possible.

P.P.S. This is not duplicate even though people made it look like that. The other question is about expressions and it's a completely special thing in C# that is not an ordinary delegate.

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • 3
    Does this answer your question? [Most efficient way to test equality of lambda expressions](https://stackoverflow.com/questions/283537/most-efficient-way-to-test-equality-of-lambda-expressions) – MyBug18 May 08 '20 at 07:33
  • I don't think so, the answer is about Expression, I have already seen it. As far as I understand Expression is a completely different thing. Perhaps my understanding is wrong though? – Ilya Chernomordik May 08 '20 at 07:39
  • Since I'm not sure why my answer isn't working for you, I've opted to delete it. I'm not sure what else to suggest. – ProgrammingLlama May 08 '20 at 08:05
  • 1
    Your answer seem to work in a general code though, why it does not work for me, I cannot figure out , I suppose it has to do something with the context of a unit test. It was a good suggestion though, thank you – Ilya Chernomordik May 08 '20 at 08:06

0 Answers0