Last active
January 23, 2017 11:58
-
-
Save yasu-s/f774c2c0649751979afb726430707bde to your computer and use it in GitHub Desktop.
LINQ使用時・未使用時のロジック比較サンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Anyサンプル | |
/// </summary> | |
private void Any() | |
{ | |
var arr = new int[] { 1, 5, 3, 8, 9 }; | |
bool result = false; | |
// LINQなし:配列に7を超える数値があればTrue | |
foreach (var num in arr) | |
{ | |
if (num > 7) | |
{ | |
result = true; | |
break; | |
} | |
} | |
Console.Write($"result={result}"); | |
// LINQあり:配列に7を超える数値があればTrue | |
result = arr.Any(num => num > 7); | |
Console.Write($"result={result}"); | |
} | |
private void All() | |
{ | |
var arr = new int[] { 1, 5, 3, 8, 9 }; | |
bool result = true; | |
// LINQなし:配列のすべての数値が7を超える場合True | |
foreach (var num in arr) | |
{ | |
if (num <= 7) | |
{ | |
result = false; | |
break; | |
} | |
} | |
Console.Write($"result={result}"); | |
// LINQあり:配列のすべての数値が7を超える場合True | |
result = arr.All(num => num > 7); | |
Console.Write($"result={result}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment