StringPool

                Never    
C#
       
namespace Test
{
	internal static class Program
	{
		private static void Main()
		{
			string s;
			// Is Interned and Is Interned Reference
			string @null = null;
			// Is Interned and Is NOT Interned Reference
			string empty = string.Copy(string.Empty);
			if ((object)empty == (object) string.Empty) throw new Exception("");
			// Is Interned and Is Interned Reference
			string interned_InternedRef = "333";
			// Is NOT Interned and Is NOT Interned Reference
			string notInterned_NotInternedRef = string.Concat(s = 9.ToString(), s, s);
			// Is Interned and Is NOT Interned Reference
			string interned_NotInternedRef = string.Concat(s = 3.ToString(), s, s);
			// It is impossible for a string to NOT be interned and is an interned reference
			// Therefore, an output of False, True is impossible.
			Out("@null", "True, True", @null);
			Out("empty", "True, False", empty);
			Out("interned_InternedRef", "True, True", interned_InternedRef);
			Out("notInterned_NotInternedRef", "False, False", notInterned_NotInternedRef);
			Out("interned_NotInternedRef", "True, False", interned_NotInternedRef);
		}
		
		private static string ToInfoString(string value)
		{
			if ((object)value == null) return "(-1)<null>";
			if (value.Length == 0) return "(0)``";
			return "(" + value.Length.ToString() + ")`" + value + "`";
		}
		
		private static void Out(string header, string correctOutput, string value)
		{
			Console.WriteLine(header + "\r\nCorrect Output:" + correctOutput + "\r\nValue:" + ToInfoString(value));
			Console.Write("--------------------\r\n");
			string interned;
			bool isInterned = StringPool.TryGetInterned(value, out interned);
			Console.WriteLine("Is Interned:" + isInterned.ToString());
			Console.WriteLine("Is Interned Reference:" + (isInterned && (object)value == (object)interned).ToString());
			Console.Write("--------------------\r\n");
		}
	} 
}

namespace RedSkies
{
	public static class StringPool
	{
		/// <summary>
		/// Retrieves the system's reference to the specified string if available; otherwise <paramref name="value" /> is returned.
		/// </summary>
		/// <returns>The system's reference to the specified string if available; otherwise <paramref name="value" />.</returns>
		public static string TryGetInterned(string value)
		{
			if ((object)value == null) return null;
			if (value.Length == 0) return string.Empty;
			string interned;
			return (object)(interned = string.IsInterned(value)) == null ? value : interned;
		}
		
		/// <summary>
		/// Replaces <paramref name="value" /> with the retrieved the system's reference to the specified string if available.
		/// </summary>
		public static void TryGetInterned(ref string value)
		{
			if ((object)value == null)
			{
				value = null;
				return;
			}
			if (value.Length == 0)
			{
				value = string.Empty;
				return;
			}
			string interned;
			if ((object)(interned = string.IsInterned(value)) == null) return;
			value = interned;
		}
		
		/// <summary>
		/// Retrieves the system's reference to the specified string if available.
		/// </summary>
		/// <returns><langword name="true" /> if the specified string is available in the system's string pool; otherwise <langword name="false" />.</returns>
		// TODO: Check IL to see if the labels create more bytes in the method. If so, then don't use the labels.
		public static bool TryGetInterned(string value, out string interned)
		{
			if ((object)value == null)
			{
				interned = null;
				goto RETURN_TRUE;
			}
			if (value.Length == 0)
			{
				interned = string.Empty;
				goto RETURN_TRUE;
			}
			if ((object)(interned = string.IsInterned(value)) == null) goto RETURN_FALSE;
		RETURN_TRUE:
			return true;
		RETURN_FALSE:
			interned = null;
			return false;
		}
	}
}

Raw Text