c# - List<T> is cleared problem -


Please take a look at the code. It should not take more time to get a glimpse.

Class Teacher {Private Ent_id; Get public IIT {return _id; } Set {_id = value; }} Private string _message; Public string message {receive (_message; return) (_message = value;}} public teacher (int id, string msg) {_id = id; _message = msg;} private list & lt; curriculum & gt; _items; public LIST & CONDITION> GetCourses () {Return_ITS;} Public Teachers () {if (_items == Faucet) {_items = New list & lt; curriculum & gt; ();} _Items.ed (new course (1, "cpp")); _items.Add (new course (1, "java"); _items.Add (new course (1, "cs"))}} show public zoom () {Console.WriteLine ( This._id); Console.WriteLine (this._message);} Generic Zero ShowList () {foreach (_Items in Course C) {C. Show ();}}} Classroom Course {Private Int _id} Public IIT {Receive {return _id;} set {_id = value;}} Personal string _message; public string message {receive (return _message;} set {_message = value;}} public course (int id, string msg) {_id = id; _message = msg;} personal list & teacher; teacher & gt; ; _items; public list & teacher; teacher & gt; GetTeachers () {return_items; } Public Course () {if (_items == faucet) {_items = New list & lt; Teacher & gt; (); } _its.ed (new teacher (1, "ttt")); _items.Add (new teacher (1, "PPP")); _items.Add (new teacher (1, "MMM")); } Show Public Zero () {Console.WriteLine (this._id); Console.WriteLine (this._message); } Public Zero ShowList () {foreach (Teacher T in _items) {T. Show (); }}} Class program {static zero principal (string [] args) {teacher t = new teacher (); TID = 1; T. Message = "hey"; T.Show (); T.ShowList (); . T.GetCourses () Clear (); T.Show (); T.ShowList (); Console.ReadLine (); }}

Since refers to GetCourse () _items, calling t.GetCourses (). clean (); is the underlying course - teacher in the list.

I want to stop this behavior. That is, GetCourse () will return a list but it will not be changeable.

How to get it?

You can make a copy of the list, or wrap it in a readon call:

  private list & lt; Curriculum & gt; _items; Public IList & lt; Curriculum & gt; GetCourses () {New List Back << Lt; Curriculum & gt; (_ item); }  

or

  private list & lt; Curriculum & gt; _items; Public IList & lt; Curriculum & gt; GetCourses () {New Readonly Collection & lt; Curriculum & gt; (_ item)); }  

The first option creates an independent list - the collar will be able to modify it, add or delete items, but those changes will not be seen in the list of teacher objects. The second option is just a cover around the current list - so any changes in the collection will be visible through the cover. The caller will not be able to make any changes to the collection.

Note that in both cases, if the course objects are referenced from the list, then their data changes, they change


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -