@Jawad is right. You can do something like this to accomplimish your goal:
var count = doc1StrArray.SelectMany(x => x).Count(c => c == CharToCount);
This selects any character in each array item and returns them as one IEnumerable<char>. When you know apply .Count(..) on it, you are counting against each character.
Edit:
I made some tests and it seems that the for-loop is the fastest
public int GetAmountOfChar(string[] strings, char character) {
int amountOfChar = 0;
var linesLength = strings.Length;
for (var lineIndex = 0; lineIndex < linesLength; lineIndex++)
{
var line = TextExample[lineIndex];
var charsLength = line.Length;
for (var charIndex = 0; charIndex < charsLength; charIndex++)
if (line[charIndex] == character)
amountOfChar++;
}
return amountOfChar;
}
but not the foreach-loop:
[TestSelectManyCount] Found 5861172 characters in 3295,8278ms
[TestForForIncrement] Found 5861172 characters in 377,0376ms
[TestForCount] Found 5861172 characters in 1064,9164ms
[TestForEachWhileIndexOfIncrement] Found 5861172 characters in 1550,894ms
[TestForEachForEachIncrement] Found 5861172 characters in 473,4347ms
[TestSumCounts] Found 5861172 characters in 1062,2644ms
using System;
using System.Diagnostics;
using System.Linq;
namespace StackOverflow._62505315
{
public partial class Program
{
public const char CharToBeFound = 'a';
public const int Iterations = 5000;
static Stopwatch stopWatch = new Stopwatch();
static void Main(string[] args)
{
TestSelectManyCount();
TestForForIncrement();
TestForCount();
TestForEachWhileIndexOfIncrement();
TestForEachForEachIncrement();
TestSumCounts();
}
public static void TestSelectManyCount()
{
var iterations = Iterations;
int amountOfChar = 0;
stopWatch.Start();
checked
{
while (iterations-- >= 0)
{
amountOfChar += TextExample.SelectMany(x => x).Count(c => c == CharToBeFound);
}
}
stopWatch.Stop();
Console.WriteLine($"[{nameof(TestSelectManyCount)}] Found {amountOfChar} characters in {stopWatch.Elapsed.TotalMilliseconds}ms");
stopWatch.Reset();
}
public static void TestForForIncrement()
{
var iterations = Iterations;
int amountOfChar = 0;
stopWatch.Start();
checked
{
while (iterations-- >= 0)
{
var linesLength = TextExample.Length;
for (var lineIndex = 0; lineIndex < linesLength; lineIndex++)
{
var line = TextExample[lineIndex];
var charsLength = line.Length;
for (var charIndex = 0; charIndex < charsLength; charIndex++)
if (line[charIndex] == CharToBeFound)
amountOfChar++;
}
}
}
stopWatch.Stop();
Console.WriteLine($"[{nameof(TestForForIncrement)}] Found {amountOfChar} characters in {stopWatch.Elapsed.TotalMilliseconds}ms");
stopWatch.Reset();
}
public static void TestForCount()
{
var iterations = Iterations;
var amountOfChar = 0;
stopWatch.Start();
checked
{
while (iterations-- >= 0)
{
var lineLength = TextExample.Length;
for (var lineIndex = 0; lineIndex < lineLength; lineIndex++)
{
amountOfChar += TextExample[lineIndex].Count(c => c == CharToBeFound);
}
}
}
stopWatch.Stop();
Console.WriteLine($"[{nameof(TestForCount)}] Found {amountOfChar} characters in {stopWatch.Elapsed.TotalMilliseconds}ms");
stopWatch.Reset();
}
public static void TestForEachWhileIndexOfIncrement()
{
var iterations = Iterations;
var amountOfChar = 0;
stopWatch.Start();
checked
{
while (iterations-- >= 0)
{
foreach (string word in TextExample)
{
int startIndex = 0;
int charIndex = 0;
while ((charIndex = word.IndexOf(CharToBeFound, startIndex)) != -1)
{
startIndex = charIndex + 1;
amountOfChar++;
}
}
}
}
stopWatch.Stop();
Console.WriteLine($"[{nameof(TestForEachWhileIndexOfIncrement)}] Found {amountOfChar} characters in {stopWatch.Elapsed.TotalMilliseconds}ms");
stopWatch.Reset();
}
public static void TestForEachForEachIncrement()
{
var iterations = Iterations;
int amountOfChar = 0;
stopWatch.Start();
checked
{
while (iterations-- >= 0)
{
foreach (var word in TextExample)
foreach (char character in word)
if (character == CharToBeFound)
amountOfChar++;
}
}
stopWatch.Stop();
Console.WriteLine($"[{nameof(TestForEachForEachIncrement)}] Found {amountOfChar} characters in {stopWatch.Elapsed.TotalMilliseconds}ms");
stopWatch.Reset();
}
public static void TestSumCounts()
{
var iterations = Iterations;
int amountOfChar = 0;
stopWatch.Start();
checked
{
while (iterations-- >= 0)
{
amountOfChar += TextExample.Sum(s => s.Count(c => c == CharToBeFound));
}
}
stopWatch.Stop();
Console.WriteLine($"[{nameof(TestSumCounts)}] Found {amountOfChar} characters in {stopWatch.Elapsed.TotalMilliseconds}ms");
stopWatch.Reset();
}
}
}
For my surprise I also found a reference here that does underline the fact, that the for-loop is outscaling the foreach-loop
return string.Join("", doc1StrArray).Aggregate(0, (i, c) => c.Equals(CharToCount) ? ++i : i);