Use lambda as function parameter in CSharp

Description

The following code shows how to use lambda as function parameter.

Example


using System;/*from w  w w.  j  a  va  2s.  co  m*/
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


public class MainClass
{
    public delegate Boolean CheckString(String Str);
    public static void Main(string[] args)
    {
        String[] TestArray = { "One", "Two", "Three", "Four", "Five" };
        String[] OutputArray = FilterStrings(TestArray, TestStr => TestStr.Length > 3);
        foreach (String ThisString in OutputArray)
            Console.WriteLine(ThisString);
    }
    public static String[] FilterStrings(String[] Input, CheckString Filter)
    {
        ArrayList ResultList = new ArrayList();
        foreach (String ThisString in Input)
        {
            if (Filter(ThisString))
                ResultList.Add(ThisString);
        }
        return (String[])ResultList.ToArray(typeof(String));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor