{"id":2239,"date":"2005-11-23T09:04:00","date_gmt":"2005-11-23T09:04:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/type-safe-collections\/"},"modified":"2016-07-28T10:48:42","modified_gmt":"2016-07-28T10:48:42","slug":"type-safe-collections","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/type-safe-collections\/","title":{"rendered":"Type safe collections"},"content":{"rendered":"<p>I must admit that creating a nice type safe collection for a new feature in one of our products took me a little time. There are some very good resources online on how to setup your own type safe collection so I thought I&#8217;d contribute a little to that resource. <\/p>\n<p> The key to all this working nicely is to set up a good base class that has a thin implementation of the <code>IList<\/code> interface with one key addition&#8230; <\/p>\n<pre>protected virtual void OnValidate(object value){ if (!(value is ListObject)) {  throw new ArgumentException(\"ListObjects only in this List.\"); }}<\/pre>\n<p> This simple function in your base class is then used by pretty much all of the <code>IList<\/code> implementation functions that take an &#8216;object&#8217; as the argument. Of course we want to make sure that this &#8216;object&#8217; is one of the correct type &#8211; we do that by calling <code>OnValidate()<\/code> like&#8230; <\/p>\n<pre>public bool Contains(object value){ OnValidate(value); return m_InnerList.Contains(value);}<\/pre>\n<p> So if you don&#8217;t pass an object of the correct type to one of the <code>IList<\/code> implementations then we throw an <code>ArgumentException<\/code> as one would expect. Now the cunning part is that we create this as part of a base class in our application and whenever we need a type safe collection we simply inherit from it and <code>override<\/code> the <code>OnValidate()<\/code> method. <\/p>\n<pre>protected override void OnValidate(object value){ base.OnValidate(value); if (!(value is FunkyObject)) {  throw new ArgumentException(\"FunkyObjects only in this List.\"); }}<\/pre>\n<p> Then if we&#8217;re being really really nice to our API users we can then go on and provide <code>IList<\/code>-like functions that take the appropriate type as an argument so they know what they&#8217;re meant to be doing. <\/p>\n<pre>public bool Contains(InheritedObject funkyNewObject){ return base.Contains(funkyNewObject);}<\/pre>\n<p> And there we have it, a simple way of producing a number of classes of type safe collections in .NET 1.1 C#. Of course this is what generics are for now although .NET 2 is still a little green.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I must admit that creating a nice type safe collection for a new feature in one of our products took me a little time. There are some very good resources online on how to setup your own type safe collection so I thought I&#8217;d contribute a little to that resource. The key to all this&#8230;&hellip;<\/p>\n","protected":false},"author":167198,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2239","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/167198"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2239"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2239\/revisions"}],"predecessor-version":[{"id":41338,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2239\/revisions\/41338"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2239"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}