Goal
Deduplicate a small, repeated vocabulary using string.Intern and demonstrate that repeated values share the same reference.
Requirements
-
Given an array of tokens with repeats (e.g.,
["GET","POST","GET","PUT","GET"]), normalize each withstring.Intern. Build at least some entries at runtime (for examplenew string("GET")or aStringBuilder) instead of writing them all as literals: repeated literals already resolve to one reference each, so an all-literal array makes the before/after count a no-op. -
Show that all
"GET"entries refer to the same reference after interning. -
Count unique references before vs after interning. Use
ReferenceEqualityComparer.InstancefromSystem.Collections.Genericrather than writing your own comparer.
Checkpoint: for the five-token array above the counts must be Before refs: 5, After refs: 3, and ReferenceEquals must be True for every pair of "GET" entries afterwards. If the “before” count is already 3 you built the array entirely from literals and the compiler interned them for you.