Start learning free

LESSON 9 OF 69 · PROJECTS: COMMAND-LINE TOOLS

Counting Lines with List.Count in C#: wc v1

Practice Read the full explanation

Build a tiny version of the Unix wc command. Given a list of lines, return the total number of lines as an integer.

Function signature:

public int CountLines(List<string> lines)

Example:

Input: lines = ["hello", "world", "foo", "bar", "baz"]

Output: 5

Every entry of the list is one line, even an empty string "". An empty list has 0 lines.


The number of lines is just the list's count. Use lines.Count.

A one-liner:
class Solution {
    public int CountLines(List<string> lines) {
        return lines.Count;
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    C# · PROJECTS: COMMAND-LINE TOOLS

    Wc V1 in C#, explained

    Problem overview

    The Unix wc command counts things in a text. Its -l flag counts lines. This first version does only that: CountLines receives a List<string>, where each entry is one line, and returns how many lines there are as an int.

    For ["hello", "world", "foo", "bar", "baz"] the answer is 5. Every entry counts as a line, even an empty string "", because a blank line is still a line. An empty list has 0 lines.

    Approach

    Real wc -l counts the newline characters in a file. This lesson works one level up: the text has already been split into lines, so the number of lines is the number of entries in the list. There is no splitting or searching left to do.

    You could write a loop with a counter and add one for every entry. That works, but a List<T> already keeps track of how many items it holds. Reading that number is a single operation, so no loop is needed.

    The same reasoning covers the special cases. A list with one empty string has one entry, so the answer is 1. An empty list has no entries, so the answer is 0. Nothing in the method has to treat them differently.

    Step by step

    1. Return the list's Count. It is a property, so you read it without parentheses. The method's return type is int, and Count is an int, so no conversion is needed.

    The solution in C#

    class Solution {
        public int CountLines(List<string> lines) {
            return lines.Count;
        }
    }
    

    Details worth knowing:

    • Count or Length? A List<T> has Count. An array (string[]) and a string have Length. Writing lines.Length on a list is a compile error that begins error CS1061: 'List<string>' does not contain a definition for 'Length'. The names differ because a list can grow and shrink, so it reports how many items it holds right now, while an array's size is fixed when it is created.
    • The LINQ method Count(). With using System.Linq;, lines.Count() also works and returns the same number. For a list it simply reads the Count property, so the property is the more direct choice and needs no extra using.
    • It counts entries, not characters. lines.Count says nothing about how long each line is. The list ["", "", ""] has three entries and no characters at all.
    • A null list fails. If lines were null, reading Count would throw a NullReferenceException. The lesson always passes a real list, possibly an empty one.
    • List<T> comes from System.Collections.Generic. In a file of your own you would need using System.Collections.Generic; at the top.

    Complexity

    Time: O(1). The list stores its item count, so reading it doesn't depend on the size of the list.

    Space: O(1). The method creates no new data.

    Edge cases and common mistakes

    • Skipping blank lines. Counting only non-empty strings would turn ["a", "", "b"] into 2, but the answer is 3.
    • Using Length. It doesn't compile for a list. Use Count.
    • Looping for nothing. A foreach with a counter gives the same result, only with more code. Prefer the property.
    • Counting newline characters. The entries in this list don't contain \n. The list already holds the lines separately, so the number of entries is the number of lines.

    Testing

    • ["hello", "world", "foo", "bar", "baz"] gives 5.
    • [] gives 0.
    • [""] gives 1.
    • ["a", "", "b"] gives 3.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free