How to use ReadOnlyCollection to make StringBuilder read-only in C#

1 Answer

0 votes
using System;
using System.Collections.ObjectModel;
using System.Text;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder[] sb = new StringBuilder[1];

            var roc = new ReadOnlyCollection<StringBuilder>(sb);

            // Error CS1061  'ReadOnlyCollection<StringBuilder>' does not contain 
            //                a definition for 'Append' 
            //roc.Append("C#").Append("Java");

            sb[0] = new StringBuilder();
            sb[0].Append("c# ").Append("java ").Append("c ").Append("c++ ");
            Console.WriteLine(sb[0]);
        }
    }
}

/*
run:
   
c# java c c++

*/

 



answered Jan 19, 2017 by avibootz

Related questions

1 answer 194 views
1 answer 177 views
1 answer 121 views
1 answer 156 views
156 views asked Jun 28, 2020 by avibootz
1 answer 148 views
148 views asked Nov 19, 2016 by avibootz
1 answer 112 views
112 views asked Dec 14, 2020 by avibootz
...