How it works
Binary search narrows a search interval repeatedly, but it depends on a sorted input using the same comparison rule as the search. A list that merely contains the right values is not sufficient. Sorting is a precondition, and changes that break the ordering invalidate that precondition for later searches.
List.BinarySearch returns an index when it finds a matching value. When it does not find one, it returns the bitwise complement of an insertion index. Applying the complement operator to that negative result recovers the position where the value can be inserted while preserving sorted order. This result carries more information than a simple missing-value flag, but callers must distinguish the two forms before using it as an ordinary index.
Walk through the example. The list begins with ten, twenty, forty, and fifty in ascending order. Searching for forty returns index two. Searching for thirty returns a negative value; complementing it produces insertion index two. Inserting thirty there creates a correctly ordered list. The final printed sequence demonstrates that both the prefix and suffix retain their existing order.
Watch the boundary. Do not complement a nonnegative found index; it already has the meaning you need. Conversely, do not pass a negative not-found result directly to Insert. Duplicate values introduce another boundary: BinarySearch can find a matching element without promising the first matching occurrence. If an operation needs lower or upper bounds among duplicates, define and implement that requirement explicitly.
Practice independently. Write an InsertIfMissing helper for a sorted list of unique integers. Test insertion at the beginning, middle, and end, plus an existing value that should not be inserted twice. Then consider a list that allows duplicates and explain why a found index alone does not specify the earliest or latest matching position. Keep the sorting comparer identical between every search and insertion operation.
A working example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
var values = new List<int> { 10, 20, 40, 50 };
Console.WriteLine(values.BinarySearch(40));
int search = values.BinarySearch(30);
Console.WriteLine(search < 0);
int position = search < 0 ? ~search : search;
Console.WriteLine(position);
values.Insert(position, 30);
Console.WriteLine(string.Join(",", values));
Expected output
2 True 2 10,20,30,40,50
A negative BinarySearch result encodes ~insertionIndex, and the input must already be sorted.