{"id":173113,"date":"2026-01-06T15:52:00","date_gmt":"2026-01-06T15:52:00","guid":{"rendered":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/"},"modified":"2026-01-06T15:52:00","modified_gmt":"2026-01-06T15:52:00","slug":"c-strings-arent-really-immutable-heres-proof","status":"publish","type":"post","link":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/","title":{"rendered":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)"},"content":{"rendered":"<div class=\"youtubomatic-video-container\"><iframe loading=\"lazy\" width=\"580\" height=\"380\" src=\"https:\/\/www.youtube.com\/embed\/WLx32xBbk-4?autoplay=1&#038;controls=1&#038;hl=en\" frameborder=\"0\" allowfullscreen><\/iframe><\/div>\n<p>C# strings are immutable\u2026 until they aren\u2019t.<\/p>\n<p>In this video, we dig into .NET string interning\u2014why identical string literals can point to the same instance in memory\u2014and how to prove it with Object.ReferenceEquals(). Then we look at the oddball API String.IsInterned(), why it returns a string (or null) instead of a bool, and how String.Intern() forces a runtime-created string into the intern pool.<\/p>\n<p>Finally, for educational purposes only, we do the \u201ccursed\u201d experiment: using AsSpan(), MemoryMarshal.GetReference(), and Unsafe.Add() to take a writable reference to the underlying UTF-16 buffer and mutate a string literal. This demonstrates how breaking string immutability can violate runtime assumptions and cause extremely subtle bugs\u2014especially because interned literals may be shared across your entire program.<\/p>\n<p>Warning: The unsafe mutation technique shown here is not supported and can corrupt hash tables, break security assumptions, and crash your process. Do not use it in production.<\/p>\n<p>Topics covered<br \/>\n\u2022 What string interning is (and why it exists)<br \/>\n\u2022 Proving interning with ReferenceEquals<br \/>\n\u2022 String.IsInterned() and String.Intern()<br \/>\n\u2022 Low-level string layout (object header, length, UTF-16 chars)<br \/>\n\u2022 Unsafe mutation via byrefs and pointer arithmetic<br \/>\n\u2022 Why mutating interned strings is particularly dangerous<\/p>\n<p>Subscribe for more low-level .NET tips.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# strings are immutable\u2026 until they aren\u2019t. In this video, we dig into .NET string interning\u2014why identical string literals can point to the same instance in memory\u2014and how to prove it with Object.ReferenceEquals(). Then we look at the oddball API String.IsInterned(), why it returns a string (or null) instead of a bool, and how String.Intern() forces a runtime-created string into the intern pool. Finally, for educational purposes only, we do&#8230;<\/p>\n","protected":false},"author":1,"featured_media":173114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","tve_updated_post":"","tve_custom_css":"","tve_user_custom_css":"","tve_globals":{},"tcb2_ready":0,"tcb_editor_enabled":0,"tve_landing_page":"","_tve_header":"","_tve_footer":""},"categories":[1],"tags":[],"class_list":["post-173113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof) - UshopWell.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof) - UshopWell.com\" \/>\n<meta property=\"og:description\" content=\"C# strings are immutable\u2026 until they aren\u2019t. In this video, we dig into .NET string interning\u2014why identical string literals can point to the same instance in memory\u2014and how to prove it with Object.ReferenceEquals(). Then we look at the oddball API String.IsInterned(), why it returns a string (or null) instead of a bool, and how String.Intern() forces a runtime-created string into the intern pool. Finally, for educational purposes only, we do...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/\" \/>\n<meta property=\"og:site_name\" content=\"UshopWell.com\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-06T15:52:00+00:00\" \/>\n<meta name=\"author\" content=\"UShopWell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"UShopWell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/\"},\"author\":{\"name\":\"UShopWell\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#\\\/schema\\\/person\\\/6fd1f9e0ff932e534c86c70d5acff0fc\"},\"headline\":\"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)\",\"datePublished\":\"2026-01-06T15:52:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/\"},\"wordCount\":211,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/173113\\\/c-strings-arent-really-immutable-heres-proof.jpg\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/\",\"url\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/\",\"name\":\"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof) - UshopWell.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/173113\\\/c-strings-arent-really-immutable-heres-proof.jpg\",\"datePublished\":\"2026-01-06T15:52:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#primaryimage\",\"url\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/173113\\\/c-strings-arent-really-immutable-heres-proof.jpg\",\"contentUrl\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/173113\\\/c-strings-arent-really-immutable-heres-proof.jpg\",\"width\":480,\"height\":360,\"caption\":\"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/c-strings-arent-really-immutable-heres-proof\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#website\",\"url\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/\",\"name\":\"UshopWell.com\",\"description\":\"The Premiere Online Marketplace\",\"publisher\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#organization\",\"name\":\"UshopWell\",\"url\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/wp-content\\\/uploads\\\/2018\\\/01\\\/pandaSwea.png\",\"contentUrl\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/wp-content\\\/uploads\\\/2018\\\/01\\\/pandaSwea.png\",\"width\":365,\"height\":359,\"caption\":\"UshopWell\"},\"image\":{\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/#\\\/schema\\\/person\\\/6fd1f9e0ff932e534c86c70d5acff0fc\",\"name\":\"UShopWell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4adb372cadd43b4d4c57964dab95b0f69618bf960d131c4acf49d96d6bbc9c6e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4adb372cadd43b4d4c57964dab95b0f69618bf960d131c4acf49d96d6bbc9c6e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4adb372cadd43b4d4c57964dab95b0f69618bf960d131c4acf49d96d6bbc9c6e?s=96&d=mm&r=g\",\"caption\":\"UShopWell\"},\"url\":\"https:\\\/\\\/ushopwell.com\\\/ublog\\\/author\\\/kburnettu\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof) - UshopWell.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/","og_locale":"en_US","og_type":"article","og_title":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof) - UshopWell.com","og_description":"C# strings are immutable\u2026 until they aren\u2019t. In this video, we dig into .NET string interning\u2014why identical string literals can point to the same instance in memory\u2014and how to prove it with Object.ReferenceEquals(). Then we look at the oddball API String.IsInterned(), why it returns a string (or null) instead of a bool, and how String.Intern() forces a runtime-created string into the intern pool. Finally, for educational purposes only, we do...","og_url":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/","og_site_name":"UshopWell.com","article_published_time":"2026-01-06T15:52:00+00:00","author":"UShopWell","twitter_card":"summary_large_image","twitter_misc":{"Written by":"UShopWell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#article","isPartOf":{"@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/"},"author":{"name":"UShopWell","@id":"https:\/\/ushopwell.com\/ublog\/#\/schema\/person\/6fd1f9e0ff932e534c86c70d5acff0fc"},"headline":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)","datePublished":"2026-01-06T15:52:00+00:00","mainEntityOfPage":{"@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/"},"wordCount":211,"commentCount":0,"publisher":{"@id":"https:\/\/ushopwell.com\/ublog\/#organization"},"image":{"@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#primaryimage"},"thumbnailUrl":"https:\/\/ushopwell.com\/ublog\/wp-content\/uploads\/2026\/01\/173113\/c-strings-arent-really-immutable-heres-proof.jpg","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/","url":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/","name":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof) - UshopWell.com","isPartOf":{"@id":"https:\/\/ushopwell.com\/ublog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#primaryimage"},"image":{"@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#primaryimage"},"thumbnailUrl":"https:\/\/ushopwell.com\/ublog\/wp-content\/uploads\/2026\/01\/173113\/c-strings-arent-really-immutable-heres-proof.jpg","datePublished":"2026-01-06T15:52:00+00:00","breadcrumb":{"@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#primaryimage","url":"https:\/\/ushopwell.com\/ublog\/wp-content\/uploads\/2026\/01\/173113\/c-strings-arent-really-immutable-heres-proof.jpg","contentUrl":"https:\/\/ushopwell.com\/ublog\/wp-content\/uploads\/2026\/01\/173113\/c-strings-arent-really-immutable-heres-proof.jpg","width":480,"height":360,"caption":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)"},{"@type":"BreadcrumbList","@id":"https:\/\/ushopwell.com\/ublog\/c-strings-arent-really-immutable-heres-proof\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ushopwell.com\/ublog\/"},{"@type":"ListItem","position":2,"name":"C# Strings Aren\u2019t Really Immutable (Here\u2019s Proof)"}]},{"@type":"WebSite","@id":"https:\/\/ushopwell.com\/ublog\/#website","url":"https:\/\/ushopwell.com\/ublog\/","name":"UshopWell.com","description":"The Premiere Online Marketplace","publisher":{"@id":"https:\/\/ushopwell.com\/ublog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ushopwell.com\/ublog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ushopwell.com\/ublog\/#organization","name":"UshopWell","url":"https:\/\/ushopwell.com\/ublog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ushopwell.com\/ublog\/#\/schema\/logo\/image\/","url":"https:\/\/ushopwell.com\/ublog\/wp-content\/uploads\/2018\/01\/pandaSwea.png","contentUrl":"https:\/\/ushopwell.com\/ublog\/wp-content\/uploads\/2018\/01\/pandaSwea.png","width":365,"height":359,"caption":"UshopWell"},"image":{"@id":"https:\/\/ushopwell.com\/ublog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/ushopwell.com\/ublog\/#\/schema\/person\/6fd1f9e0ff932e534c86c70d5acff0fc","name":"UShopWell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4adb372cadd43b4d4c57964dab95b0f69618bf960d131c4acf49d96d6bbc9c6e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4adb372cadd43b4d4c57964dab95b0f69618bf960d131c4acf49d96d6bbc9c6e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4adb372cadd43b4d4c57964dab95b0f69618bf960d131c4acf49d96d6bbc9c6e?s=96&d=mm&r=g","caption":"UShopWell"},"url":"https:\/\/ushopwell.com\/ublog\/author\/kburnettu\/"}]}},"_links":{"self":[{"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/posts\/173113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/comments?post=173113"}],"version-history":[{"count":0,"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/posts\/173113\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/media\/173114"}],"wp:attachment":[{"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/media?parent=173113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/categories?post=173113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ushopwell.com\/ublog\/wp-json\/wp\/v2\/tags?post=173113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}