Skip to content

Instantly share code, notes, and snippets.

@csdear
Created July 17, 2015 22:03
Show Gist options
  • Select an option

  • Save csdear/e7738b165f07ec6ed3d1 to your computer and use it in GitHub Desktop.

Select an option

Save csdear/e7738b165f07ec6ed3d1 to your computer and use it in GitHub Desktop.
Anonymous Function - Anonymous Method
//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