Express lambda expression for Func <IObserver <char>, IDisposable>

I read the following example in C # 5.0 Program. Why can I convert this lambda expression to Func<IObserver<char>, IDisposable> when it does not return IDisposable.

 IObservable<char> singularHotSource = Observable.Create( (Func<IObserver<char>, IDisposable>)(obs => { while (true) { obs.OnNext(Console.ReadKey(true).KeyChar); } })); 
+4
source share
1 answer

As @Patryk notes, this is basically the same as the question: β€œWhy is it allowed to say that it returns int when it is not?”:

  int SomeMethod() { while (true) { } } 

And the answer; it doesn’t return something else on any code path (another type or void ) - so sure why not; the compiler can represent this as a method that returns an int .

+3
source

Source: https://habr.com/ru/post/1494227/


All Articles