OLD
class Program
{
static void Main()
{
string[] a = { null, "タロウ", null, "ハナコ" };
foreach (var n in a)
{
if (n != null) {
System.Diagnostics.Debug.WriteLine(n + "さん");
}
}
}
}
NEW
using System.Linq;
class Program
{
static void Main()
{
string[] a = { null, "タロウ", null, "ハナコ" };
foreach (var n in a.Where((c) => c != null))
{
if (n != null)
{
System.Diagnostics.Debug.WriteLine(n + "さん");
}
}
}
}
Result
タロウさんハナコさん
・ループ内if文を追放できる
引用元:
C#ショートプログラミング
0 件のコメント:
コメントを投稿