Exemplo n.º 1
0
    static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        NumberPredicate evenPredicate = IsEven;

        // call a method using delegate
        Console.WriteLine($"Use a delegate variable: {evenPredicate(6)}");

        // create an instance of the NumberPredicate delegate type using an
        // implicit lambda expression
        evenPredicate = number => number % 2 == 0;

        // call a lambda expression through a variable
        Console.WriteLine($"Use a lambda-expression variable: {evenPredicate(4)}");

        // filter the even numbers using a lambda expression
        List <int> evenNumbers = FilterArray(numbers, evenPredicate);

        // display the result
        DisplayList("Use a lambda expression to filter even numbers: ", evenNumbers);

        // filter the odd numbers using an explicitly typed lambda
        // expression
        List <int> oddNumbers = FilterArray(numbers, (int number) => number % 2 == 1);

        // display the result
        DisplayList("Use a lambda expression to filter odd numbers: ", oddNumbers);

        // filter numbers greater than 5 using an implicit lambda statement
        List <int> numbersOver5 = FilterArray(numbers, number => { return(number > 5); });

        // display the result
        DisplayList("Use a lambda expression to filter numbers over 5: ", numbersOver5);
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            //create an instance of the NumberPredicate delegate type
            NumberPredicate evenPredicate = IsEven;

            //Call IsEven eusing a delegate variable
            Console.WriteLine(
                $"Call IsEven using a delegate variable: {evenPredicate}");

            //filter the even numbers using method IsEven
            List <int> evenNumbers = FilterArray(numbers, evenPredicate);

            //display the result
            DisplayList("Use IsEven to filter even numbers: ", evenNumbers);

            //filter the odd numbers using method IsOdd
            List <int> oddNumbers = FilterArray(numbers, IsOdd);

            //display the result
            DisplayList("Use IsOdd to filter odd numbers: ", oddNumbers);

            //filter numbers greater than 5 using method IsOver5
            List <int> numbersOver5 = FilterArray(numbers, IsOver5);

            //display the result
            DisplayList("Use IsOver5 to filter numbers over 5: ", numbersOver5);
            Console.Read();
        }
        //Event handler for evenOddButton
        private void evenOddButton_Click(object sender, RoutedEventArgs e)
        {
            //create an instance for the delegate
            NumberPredicate evenPredicate = IsEven;

            //try to parse the input into a integer
            int number;

            if (Int32.TryParse(numberTextBox.Text, out number))
            {
                if (evenPredicate(number) == true)
                {
                    errorMessageLabel.Content = null;
                    evenOddLabel.Content      = "Even";
                }
                evenPredicate = IsOdd;
                if (evenPredicate(number) == true)
                {
                    errorMessageLabel.Content = null;
                    evenOddLabel.Content      = "Odd";
                }
            }
            else
            {
                evenOddLabel.Content      = null;
                errorMessageLabel.Content = "Please check the value of the (2) number. Must be an integer.";
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20, 21, 30, 32, 36, 40, 50, 60, 70, 80, 90, 100 };

            // create an instance of the NumberPredicate elegate type
            NumberPredicate evenPredicate = IsEven;

            // Call IsEven using a delegate variable
            Console.WriteLine(
                $"Call IsEven using a delegate variable : {evenPredicate(4)}");

            // Filter the even numbers using method IsEven
            List <int> evenNumbers = FilterArray(numbers, evenPredicate);

            // Display the result
            DisplayList("\nUse IsEven to filter even numbers: ", evenNumbers);

            // Filter the odd numbers using method IsOdd
            List <int> oddNumbers = FilterArray(numbers, IsOdd);

            // Display the result
            DisplayList("\nUse IsOdd to filter odd numbers: ", oddNumbers);

            // Filter numbers greater than 5 using method IsOver5
            List <int> numbersOver5 = FilterArray(numbers, IsOver5);

            // Display the result
            DisplayList("\nUse IsOver5 to filter numbers over 5: ", numbersOver5);

            // Filter numbers between 5 and 50
            List <int> numbersBetween5and50 = FilterArray(numbers, Between5and50);

            // Display the result
            DisplayList("\nUse Getween5and50 to filter numbers between 5 and 50: ", numbersBetween5and50);

            // Filter numbers that are a multiple of 10
            List <int> numbersMultiple10 = FilterArray(numbers, IsMultiple10);

            // Display the result
            DisplayList("\nUse IsMultiple10 to filter numbers that are a multiple of 10: ", numbersMultiple10);

            // Filter numbers that are not a multiple of 10
            List <int> numbersNot10 = FilterArray(numbers, IsNotMult10);

            // Display the result
            DisplayList("\nUse IsNotMulti10 to filter numbers that are not a multiple of 10: ", numbersNot10);

            // Filter numbers that are a multiple of 3
            List <int> numbersMultiple3 = FilterArray(numbers, IsMultiple3);

            // Display the result
            DisplayList("\nUse IsMultiple3 to filter numbers that are a multiple of 3: ", numbersMultiple3);

            // Filter numbers that are not a multiple of 3
            List <int> numbersNot3 = FilterArray(numbers, IsNotMult3);

            // Display the result
            DisplayList("\nUse IsNotMult3 to filter numbers that are not a multiple of 3: ", numbersNot3);
        }   // End Main()
Exemplo n.º 5
0
        public static List<int> FilterArray(int[] intArray, NumberPredicate predicate)
        {
            //hold selected elements
            List<int> result = new List<int>();

            //iterate over each element in the array
            foreach (int item in intArray)
            {
                if (predicate(item))
                    result.Add(item);
            }

            return result;
        }
Exemplo n.º 6
0
        private List <int> FilterArray(IEnumerable <int> array, NumberPredicate predicate)
        {
            var result = new List <int>();

            foreach (var num in array)
            {
                if (predicate(num))
                {
                    result.Add(num);
                }
            }

            return(result);
        }
Exemplo n.º 7
0
    // select an array's elements that satisfy the predicate
    private static List<int> FilterArray( int[] intArray,
        NumberPredicate predicate)
    {
        // hold the selected elements
          List< int > result = new List< int >();

          // iterate over each element in the array
          foreach ( int item in intArray )
          {
         // if the element satisfies the predicate
         if ( predicate( item ) )
            result.Add( item ); // add the element to the result
          } // end foreach

          return result; // return the result
    }
Exemplo n.º 8
0
 //Task C start -Check for odd and even
 #region Task C -check odd/Even Num
 private void btnEvenOdd_Click(object sender, EventArgs e)
 {
     //Exception Handelling for Number input
     try
     {
         InputValidation(txtInputNum.Text);
         string          txtBoxValue   = txtInputNum.Text;
         int             inputNum      = Convert.ToInt32(txtBoxValue);
         NumberPredicate evenPredicate = IsEven;
         lblEvenOddResult.Text  = "The Number is a : ";
         lblEvenOddResult.Text += evenPredicate(inputNum) ? "Even" : "Odd";
     }
     catch (InputException ie)
     {
         MessageBox.Show(ie.Message, "Input Alert");
     }
 }
Exemplo n.º 9
0
        }   // End Main()

        // Select an array's element that statiesfies the predicate
        private static List <int> FilterArray(int[] intArray, NumberPredicate predicate)
        {
            // Hold the selected elements
            var result = new List <int>();

            // Iterate over each element in the array
            foreach (var item in intArray)
            {
                // If the element satisfies the predicate
                if (predicate(item))  // Invokes method referenced by the predicate
                {
                    result.Add(item); // Add the element to the result
                }                     // End if
            }                         // End foreach

            return(result);           // Returns the result
        }                             // End List<>
Exemplo n.º 10
0
    // select an array's elements that satisfy the predicate
    private static List <int> FilterArray(int[] intArray, NumberPredicate predicate)
    {
        // hold the selected elements
        var result = new List <int>();

        // iterate over each element in the array
        foreach (var item in intArray)
        {
            // if the element satisfies the predicate
            if (predicate(item))
            {
                result.Add(item); // add the element to the result
            }
        }

        return(result); // return the result
    }