Sunday, January 19, 2014

Is this encapsulation? read only?

        class TestEncaps
        {
            private List<string> s;
         
            public TestEncaps()
            {
                s = new List<string>();
                s.Add("blah");
            }

            public void print()
            {
                Console.WriteLine(s[0]); 
            }

            public List<string> S
            {
                get { return s; }
            }




        }
       
        static void Main(string[] args)
        {

            TestEncaps te = new TestEncaps();
            te.S[0]= "blah!";

            te.print(); 


p.s.

this is not a solution


            public List<string> S
            {
                get {
                    string[] s2 = new string[s.Count];
                    s.CopyTo(s2);
                    return s2.ToList();

                }
            }


because CopyTo makes a shallow copy

No comments:

Post a Comment