Created
July 17, 2015 22:03
-
-
Save csdear/e7738b165f07ec6ed3d1 to your computer and use it in GitHub Desktop.
Anonymous Function - Anonymous Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //using System; | |
| //using System.Collections.Generic | |
| //derived from http://www.dotnetperls.com/anonymous-function | |
| //ON section 'Anonymous Functions' | |
| /* | |
| KEY POINTS | |
| The FindAll method used here in the anonyMethod syntax allows for filtering | |
| of a list. This would be handy in the future if you have a list data type, | |
| and you want to filter, based on MULTIPLE CONDITIONS. | |
| • AnonyFunctions/Methods use delegates | |
| • Lambda though clearer, not as robust if you need to test for | |
| multiple statements. | |
| Syntax here provides more powerful method bodies. | |
| */ | |
| class Program { | |
| static void Main() | |
| { | |
| //roboRageValues list denoting a robot's propensity for violence. | |
| List<int> roboRageValues = new List<int>() { 1, 10, 1, 2, 3}; | |
| //This syntax enables you to embed multiple statements in a anonymous function. | |
| List<int> rageLimit = roboRageValues.FindAll(delegate(int element) | |
| { | |
| if (element > 10) | |
| { | |
| throw new ArgumentException("WARNING: KILLER ROBOT DETECTED"); | |
| } | |
| if (element == 9) | |
| { | |
| throw new ArgumentException("WARNING: Approaching Rage Threshold"); | |
| } | |
| return element > 1; | |
| }); | |
| //Display the results | |
| foreach (int value in rageLimit) | |
| { | |
| Console.WriteLine(value); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment