LoginSignup
6
7

More than 5 years have passed since last update.

【C#】ラムダ式おぼえがき

Posted at

ラムダ式で文字数が5文字以下のnamesが何件あるか調べる

Sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {

        static void Main(string[] args)
        {
            List<String> names = new List<string>
            {
                "shimakaze",
                "amatsukaze",
                "kongo",
                "kuma",
                "tama",
            };

            // ラムダ式
            Func<string, bool> predicate = str => str.Length < 5;

            int count = CountList(names, predicate);
            Console.WriteLine(count);

        }

        private static int CountList(List<string> names, Func<string, bool> predicate)
        {
            int count = 0;
            foreach (string str in names)
            {
                if (predicate(str))
                {
                    count++;
                }
            }
            return count;
        }
    }
}
6
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7