Wrap a delegate in an IEqualityComparer

On the importance of GetHashCode Others have already commented on the fact that any custom IEqualityComparer<T> implementation should really include a GetHashCode method; but nobody’s bothered to explain why in any detail. Here’s why. Your question specifically mentions the LINQ extension methods; nearly all of these rely on hash codes to work properly, because they … Read more

What is Func, how and when is it used

Think of it as a placeholder. It can be quite useful when you have code that follows a certain pattern but need not be tied to any particular functionality. For example, consider the Enumerable.Select extension method. The pattern is: for every item in a sequence, select some value from that item (e.g., a property) and … Read more

Uses of Action delegate in C# [closed]

Here is a small example that shows the usefulness of the Action delegate using System; using System.Collections.Generic; class Program { static void Main() { Action<String> print = new Action<String>(Program.Print); List<String> names = new List<String> { “andrew”, “nicole” }; names.ForEach(print); Console.Read(); } static void Print(String s) { Console.WriteLine(s); } } Notice that the foreach method iterates … Read more

How do I set up a simple delegate to communicate between two view controllers?

Simple example… Let’s say the child view controller has a UISlider and we want to pass the value of the slider back to the parent via a delegate. In the child view controller’s header file, declare the delegate type and its methods: ChildViewController.h #import <UIKit/UIKit.h> // 1. Forward declaration of ChildViewControllerDelegate – this just declares … Read more

Super-simple example of C# observer/observable with delegates

The observer pattern is usually implemented with events. Here’s an example: using System; class Observable { public event EventHandler SomethingHappened; public void DoSomething() => SomethingHappened?.Invoke(this, EventArgs.Empty); } class Observer { public void HandleEvent(object sender, EventArgs args) { Console.WriteLine(“Something happened to ” + sender); } } class Test { static void Main() { Observable observable = … Read more

Delegates in swift?

Here’s a little help on delegates between two view controllers: Step 1: Make a protocol in the UIViewController that you will be removing/will be sending the data. protocol FooTwoViewControllerDelegate:class { func myVCDidFinish(_ controller: FooTwoViewController, text: String) } Step2: Declare the delegate in the sending class (i.e. UIViewcontroller) class FooTwoViewController: UIViewController { weak var delegate: FooTwoViewControllerDelegate? … Read more

How can I clear event subscriptions in C#?

From within the class, you can set the (hidden) variable to null. A null reference is the canonical way of representing an empty invocation list, effectively. From outside the class, you can’t do this – events basically expose “subscribe” and “unsubscribe” and that’s it. It’s worth being aware of what field-like events are actually doing … Read more

Delegates: Predicate vs. Action vs. Func

Predicate: essentially Func<T, bool>; asks the question “does the specified argument satisfy the condition represented by the delegate?” Used in things like List.FindAll. Action: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically. Func: Used extensively in LINQ, usually to transform the argument, e.g. by … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)