The text of this article is not in this database — only its details are. Read it on the old site: Delegates In C# Explained
2 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Delegates In C# Explained
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
Konstantinos AdamopoulosPosted Dec 20, 2022, 11:39 AM
In your second example you are checking if iPhoneX and OnePlus8 both run on IOS. Of course the former is an IOS phone and the latter is an Android phone, so the answer should correctly be false, i.e. "Not all of them runs on IOS!!!". The problem with your example is that if you reverse the order in which the two method references are added to "areBothRunsApple" delegate variable, then the brogram will return true, i.e. "Both runs on IOS!!!", which of course is wrong. This is because when you have a delegate invocation list holding more than one method references when the delegate is invoked it will invoke all the methods in its invocation list and will return the value of the last method invoked. So if you add the GetOnePlus8 method to the delicate invocation list first, and then the GetIPhoneX method second, that second method will get invoked last and will return true, which is wrong. I hope this helps :-)