---
title: "Detect a missing newline at the end of a file in a C# diff"
url: https://recursiveai.org/onediff/cookbook/detect-final-newline-change/
description: "Detect a missing final newline in C# with a unified diff. See which version lost its line ending, even when the visible text is identical."
version: "1.0.1"
---

# Detect a missing newline at the end of a file in a C# diff

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/detect-final-newline-change/detect-final-newline-change.zip)

Two files can look identical even if only one ends with a line break.

```shell
dotnet add package RecursiveAI.OneDiff
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneDiff;
Console.Write(Diff.Unified("ready\n", "ready"));
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/detect-final-newline-change/detect-final-newline-change.zip), then `dotnet run`.

Prints a unified diff with a marker beneath the line that lost its newline:

```text
--- a
+++ b
@@ -1 +1 @@
-ready
+ready
\ No newline at end of file
```

`--- a` labels the old text and `+++ b` labels the new text. The marker follows `+ready`, so it’s the new line that has no terminator.

## Variations

**Compare two files instead of string literals.** Use your own files and pass the paths as `dotnet run -- before.txt after.txt`. To reproduce the same change, save `ready` with a final LF in `before.txt` and without one in `after.txt`. Replace the print line with:

```csharp
Console.Write(Diff.Unified(
    File.ReadAllText(args[0]),
    File.ReadAllText(args[1]),
    args[0], args[1]));
```

If a path doesn’t exist, `File.ReadAllText` throws. If the text is identical, `Diff.Unified` returns an empty string and the program prints nothing.

**Show a newline being added.** Reverse the inputs. The marker now sits under the `-` line, identifying the old version as the one without a newline.

```csharp
Console.Write(Diff.Unified("ready", "ready\n"));
```

```text
--- a
+++ b
@@ -1 +1 @@
-ready
\ No newline at end of file
+ready
```

## Related recipes

- [Write a line diff as Markdown](https://recursiveai.org/onediff/cookbook/write-line-diff-markdown-report/)
- [Summarize a unified patch without applying it](https://recursiveai.org/onediff/cookbook/summarize-unified-patch-without-applying/)

## The short answer

Read each file as one string with `File.ReadAllText` and pass the strings, old first, to `Diff.Unified`. OneDiff compares lines with their terminators still attached. In the output, `\ No newline at end of file` describes the line immediately above it: after a `+` it describes the new version, and after a `-` it describes the old version.

## What does “No newline at end of file” mean in a diff?

The line above the marker ends at the last character of its text instead of ending with a line feed (`\n`). It is not a warning that there’s a blank line missing, and it isn’t text the patch adds to the file. For example, `ready\n` contains one terminated line; `ready\n\n` contains another, empty line after it. The example at the top compares `ready\n` with `ready`, so the visible text is the same but the two lines don’t match. The unified diff shows a deletion and an insertion to describe that one-character change. The [OneDiff line-token notes](https://recursiveai.org/onediff/docs/diff-algorithms/#lines-and-the-final-newline) explain why the terminator is part of the comparison.

The marker doesn’t necessarily mean a newline was **removed**. If you change the last line’s text and neither version has a final newline, the unified diff marks both the deleted line and the inserted line as unterminated. Look at where the marker appears before treating it as evidence that an editor added or removed a line ending.

## How do you preserve the final newline when reading a file in C#?

Use `File.ReadAllText` as in the variation above. [`File.ReadAllLines`](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalllines) returns the lines without their terminating characters, so it gives the same array for `ready` and `ready\n`. Joining that array back into a string doesn’t recover the original ending. Avoid trimming the text before comparing it, too: that removes the very difference you’re looking for.

`Diff.Unified` splits at `\n`; a preceding `\r` stays part of the line. If both versions have a final newline but one uses CRLF (`\r\n`) and the other uses LF (`\n`), they can still show as different without the missing-newline marker. If you want to disregard Windows-versus-Unix line endings but still catch a missing final break, replace `\r\n` with `\n` on both input strings without adding or trimming any characters at the end.

## Should there be a newline at the end of a file?

Follow the convention for that file format and project. For source code that your team wants to keep newline-terminated, [EditorConfig’s `insert_final_newline` setting](https://spec.editorconfig.org/#supported-pairs) lets the team configure that preference in the repository. For a comparison meant to record exactly what changed, compare the original texts first. Automatically adding a newline before the comparison would hide a missing one.
