surfmuggle
surfmuggle

Reputation: 5954

Use delegate without or with an assignment

Using delegates is there a benefit creating a dedicated variable instead of just using the function name?

This extract from a full code example is based upon the video: How FP Made Me Understand Design Patterns Better by Zoran Horvat.


public static PasswordPolicy AtLeast(int length) => 
                      input => input.Length >= length;

public static PasswordPolicy ContainsUpperLetter => 
                      input => input.Any(char.IsUpper);

PasswordPolicy minLengthPolicy = AtLeast(8);
PasswordPolicy upperLetterPolicy = ContainsUpperLetter;

PasswordPolicy combinedPolicy = minLengthPolicy // or AtLeast(8)
     .And(ContainsUpperLetter) // or upperLetterPolicy
     .And(specialCharPolicy);

If wonder if there is any difference or benefit in using minLengthPolicy and upperLetterPolicy versus AtLeast(8) and ContainsUpperLetter

AtLeast(8)
// versus
minLengthPolicy 
// and 
.And(ContainsUpperLetter) 
// versus
.And(upperLetterPolicy) 

Since the syntax highlighting is not working 100% here a screenshot of and a full code example for Netpad

Netpad using delegates

Upvotes: 0

Views: 72

Answers (0)

Related Questions