示例#1
0
        private static void WriteToCharArrayExamples()
        {
            Console.WriteLine("Examples of overloads of method \"ToCharArray\".");

            CustomString str = CustomString.CreateInstance("Hello world".ToCharArray());

            Console.WriteLine($"{nameof(str)} = CreateInstance(\"{str}\")");

            Console.WriteLine($"str.ToCharArray() => {ArrayToString(str.ToCharArray())}");
            Console.WriteLine($"str.ToCharArray(6, 5) => {ArrayToString(str.ToCharArray(6, 5))}");
            Console.Write("str.ToCharArray(6, 6) => ");
            try
            {
                Console.Write(ArrayToString(str.ToCharArray(6, 6)));
            }
            catch (ArgumentOutOfRangeException)
            {
                Console.Write(nameof(ArgumentOutOfRangeException));
            }
            finally
            {
                Console.WriteLine();
            }

            string ArrayToString(char[] arr)
            {
                StringBuilder stringBuilder = new StringBuilder();

                foreach (var val in arr)
                {
                    stringBuilder.Append($"[{val}] ");
                }

                return(stringBuilder.ToString());
            }

            WriteSeparatorStringsAndAwaitMessage();
        }