2 Replies - 4628 Views - Last Post: 07 January 2009 - 07:07 PM Rate Topic: -----

#1 Zammy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Return readonly reference to collection

Post icon  Posted 07 January 2009 - 06:12 PM

	   private static List<Entity> entities = new List<Entity>(100);
		public static List<Entity> Entities
		{
			get { return entities; }
		}



I have a straightforward question. I want to make this static list readonly from outside.
Unfortunately I cannot do this with the implementation I have shown. Only thing I disallow with
get-only property is not to modify the reference to the list.

I want to return a reference to the list that can be used only for reading to the collection bot not writing.
Is that possible and how? (a little bit like const keyword in c++ if I am not wrong)

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Return readonly reference to collection

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Return readonly reference to collection

Posted 07 January 2009 - 07:02 PM

See List<T>.AsReadOnly Method

This post has been edited by n8wxs: 07 January 2009 - 07:03 PM

Was This Post Helpful? 1
  • +
  • -

#3 Zammy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: Return readonly reference to collection

Posted 07 January 2009 - 07:07 PM

Exactly what I needed. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1