<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx</link><description>Lots of good comments on my previous post. To briefly follow up: One of the downsides of immutable tree implementations is that usually the tree must be built from the leaves up, which is not always convenient. We'll look at implementations which hide</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6810110</link><pubDate>Thu, 20 Dec 2007 00:55:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6810110</guid><dc:creator>Vadmyst</dc:creator><description>&lt;p&gt;Great series!!&lt;/p&gt;
&lt;p&gt;Tree traversal can also be done without using stack. For example, &lt;a rel="nofollow" target="_new" href="http://vadmyst.blogspot.com/2007/09/non-recursive-binary-tree-traversal.html"&gt;http://vadmyst.blogspot.com/2007/09/non-recursive-binary-tree-traversal.html&lt;/a&gt;. But, this approach makes tree node more &amp;quot;heavy&amp;quot; as additional information has to be used.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6810771</link><pubDate>Thu, 20 Dec 2007 01:54:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6810771</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;In your approach how do you traverse the tree for a second time? All the nodes will be marked as visited.&lt;/p&gt;
&lt;p&gt;I think what you want to do is store the &amp;quot;visited&amp;quot; information in a separate dictionary object which is owned by the traverser.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6811521</link><pubDate>Thu, 20 Dec 2007 03:41:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6811521</guid><dc:creator>Dean Harding</dc:creator><description>&lt;p&gt;You could make Visited an integer, incrementing it each time you visit a node.&lt;/p&gt;
&lt;p&gt;Obviously, it also doesn't work for immutable trees :-)&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6811532</link><pubDate>Thu, 20 Dec 2007 03:43:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6811532</guid><dc:creator>Dean Harding</dc:creator><description>&lt;p&gt;I should say &amp;quot;incrementing it each time you TRAVERSE THE TREE&amp;quot;...&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6815238</link><pubDate>Thu, 20 Dec 2007 12:37:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6815238</guid><dc:creator>Vadmyst</dc:creator><description>&lt;p&gt;Dictionary approach seems to be better, in scenarios when traversing is done only with search purpose. In this case not all nodes will be visited and &amp;quot;visited&amp;quot; counter can be useless.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6817942</link><pubDate>Thu, 20 Dec 2007 18:02:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6817942</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;Perhaps a nice feature of a future C# would be to allow a function to delegate the yield ability to some deeper function. That would allow you to write InOrder like this maybe:&lt;/p&gt;
&lt;p&gt;public static IEnumerable&amp;lt;T&amp;gt; InOrder&amp;lt;T&amp;gt;(this IBinaryTree&amp;lt;T&amp;gt; tree) { yield tree.left.InOrder(); yield return tree.value; yield tree.right.InOrder(); }&lt;/p&gt;
&lt;p&gt;Of course that's not a trivial feature to implement, but maybe someday C# will have continuations.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6819609</link><pubDate>Thu, 20 Dec 2007 21:29:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6819609</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;The InOrder function should be checking against IsEmpty instead of null or Count &amp;gt; 0.&lt;/p&gt;
&lt;p&gt;And speaking of null, it would be a nice feature to be able to specify that a variable of a class can not be set to null. &amp;nbsp;This would simplify code so that we would not have to check that a value passed into a method is null because it would not be allowed by the compiler. &amp;nbsp;This would require being able to identity a default value so that an array could be initialized. &amp;nbsp;In the case of these immutible data structues, the default would of course by Empty.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6820486</link><pubDate>Thu, 20 Dec 2007 23:45:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6820486</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;&amp;quot;I would very much like to add new syntax to a hypothetical future version of C# which would be a syntactic sugar for the code above.&amp;quot;&lt;/p&gt;
&lt;p&gt;It's called F#.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6820977</link><pubDate>Fri, 21 Dec 2007 00:55:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6820977</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Whoops, that's what I get for modifying the code from another project and posting it without compiling it. Thanks for the note.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6857136</link><pubDate>Tue, 25 Dec 2007 04:26:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6857136</guid><dc:creator>Yuval Gilboa</dc:creator><description>&lt;p&gt;I've a small question unrelated to this immutables series, but I hope is still relevant for this blog:&lt;/p&gt;
&lt;p&gt;Why did you not include generic properties/indexers in C#? Are you contemplating doing so in the &amp;quot;hypothetical future version of C#&amp;quot;.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6879247</link><pubDate>Thu, 27 Dec 2007 21:39:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6879247</guid><dc:creator>mattiasw</dc:creator><description>&lt;p&gt;Why do we have to bother about &amp;quot;out-of-stack&amp;quot;. Using the stack for recursion in trees are done in functional programming like F#, Lisp all the time.&lt;/p&gt;
&lt;p&gt;The only datastructure too big to use the stack is lists and similar, which can be several thousand elements. Trees on the other seldom becomes deeper than 100 levels, especially if you use balanced trees like AVL or red-black trees.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6909144</link><pubDate>Sun, 30 Dec 2007 22:17:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6909144</guid><dc:creator>Ifeanyi Echeruo [MSFT]</dc:creator><description>&lt;p&gt;Stackless in-order binary tree traversal with left, right and parent pointers only&lt;/p&gt;
&lt;p&gt;Off the top of my head and untested&lt;/p&gt;
&lt;p&gt;class Node {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Node Parent;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Node Left;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Node Right;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Object Value;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;class InOrderTreeWalk {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool descendLeft;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Node current;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;InOrderTreeWalk(Node start) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.descendLeft = true;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.current = start;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool moveNext() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if(this.current == null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if(this.descendLeft) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.descendLeft = false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(this.current.left != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.current = this.current.left;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(this.current.right != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.current = this.current.right;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(this.current.left != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.current = this.current.left;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;else {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(this.current.parent != null &amp;amp;&amp;amp; this.current == this.current.parent.left) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.current = this.current.parent;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(this.current.parent != null &amp;amp;&amp;amp; this.current == this.current.parent.right) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.current = this.current.parent;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(this.current.parent == null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.current = null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return this.current != null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;object getValue() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return (this.current != null) ? this.current.Value : null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6941902</link><pubDate>Tue, 01 Jan 2008 23:30:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6941902</guid><dc:creator>Smirnov</dc:creator><description>&lt;p&gt;Note that in Ifeanyi's code you are still using an additional O(n) memory for the parent pointers vs Eric's b-tree implementation. A binary tree already takes up O(n) memory for n nodes though, so you could just add it together and get back O(n) total memory usage. This doesn't sound so bad in theory.&lt;/p&gt;
&lt;p&gt;On the other hand, if you are using a balanced binary search tree, with recursion or explicit stack a traversal will only use O(log n) for extra memory. In practice it will then use less memory, as you'd have to run O(n/logn) traversals concurrently for it to actually use more.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6944101</link><pubDate>Wed, 02 Jan 2008 02:28:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6944101</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;How are you going to build an immutable binary tree that has parent pointers?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#6944111</link><pubDate>Wed, 02 Jan 2008 02:29:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6944111</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Note that &amp;quot;binary tree&amp;quot; and &amp;quot;b tree&amp;quot; are two entirely different data structures. &amp;quot;b tree&amp;quot; is NOT short for &amp;quot;binary tree&amp;quot;.&lt;/p&gt;
</description></item><item><title>Immutability in C#</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#7153119</link><pubDate>Sat, 19 Jan 2008 00:05:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7153119</guid><dc:creator>&lt;a href="http://weblogs.asp.net/bleroy"&gt;Tales from the Evil Empire&lt;/a&gt;</dc:creator><description>&lt;p&gt;For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#7166993</link><pubDate>Sun, 20 Jan 2008 04:59:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7166993</guid><dc:creator>Smirnov</dc:creator><description>&lt;p&gt;I didn't realize that b-tree was an acronym for another data structure. I meant to say binary tree there. &lt;/p&gt;
&lt;p&gt;As for building immutable trees with parent pointers, I was not suggesting that, but pointing out that Ifeanyi's binary tree class has parent pointers, whereas Eric's binary tree class does not. The point was that including a parent pointer misleadingly looks like it uses less memory to traverse, when it actually grows at a faster rate than the stack usage.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#7683700</link><pubDate>Thu, 14 Feb 2008 04:35:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7683700</guid><dc:creator>wesele</dc:creator><description>&lt;p&gt;why?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;current = stack.Peek();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;stack = stack.Pop();&lt;/p&gt;
&lt;p&gt;why not?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;current = stack.Pop();&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#7684348</link><pubDate>Thu, 14 Feb 2008 05:16:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7684348</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Because stack.Pop() returns the popped stack. &amp;nbsp;Remember, it's an immutable stack; popping it does not change the stack, popping it returns you an entirely different object which represents the state of the popped stack. &lt;/p&gt;
&lt;p&gt;More generally, immutable data structures enable the abstract data type implementer to have every method do exactly one thing, which is good design. &amp;nbsp;Popping and peeking are two entirely different things. One creates a new data structure, the other inspects an existing data structure. So why should they be the same method?&lt;/p&gt;
&lt;p&gt;Because mutable stacks are, well, mutable, the implementer of the ADT must somehow work around the inability of callers to reason about the state of the data structure. For this reason, it is very common to see mutable data structures where one method does the work properly done by many different methods, in order to guarantee atomicity.&lt;/p&gt;
&lt;p&gt;This question has come up in almost every part of this series. See the comments to the other parts for more details.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#7684375</link><pubDate>Thu, 14 Feb 2008 05:21:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7684375</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; Why did you not include generic properties/indexers in C#? &lt;/p&gt;
&lt;p&gt;Is there a compelling customer scenario for doing so which cannot already be easily solved with a generic method? I am not aware of any, but I am happy to hear any such scenario you've got. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;gt; Are you contemplating doing so in the &amp;quot;hypothetical future version of C#&amp;quot;.&lt;/p&gt;
&lt;p&gt;Not at this time, no. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Seven: More on Binary Trees</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/19/immutability-in-c-part-seven-more-on-binary-trees.aspx#8611384</link><pubDate>Tue, 17 Jun 2008 16:23:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8611384</guid><dc:creator>Duarte Cunha Leão</dc:creator><description>&lt;p&gt;Is there a compelling customer cenario for using properties, that cannot already be solved with a method?? &lt;/p&gt;
&lt;p&gt;I am not aware of any, but I would be very happy if you would give us one.&lt;/p&gt;
&lt;p&gt;It's all a matter of syntax. &lt;/p&gt;
&lt;p&gt;What led to the creation of generic methods would also lead to the creation of generic properties.&lt;/p&gt;
&lt;p&gt;Yet, I agree that the syntax could become strange...:&lt;/p&gt;
&lt;p&gt;public interface IServiceContainer&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bool &amp;nbsp; &amp;nbsp; Contains&amp;lt;TService&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TService GetInstanceOf&amp;lt;TService&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;void SetInstanceOf&amp;lt;TService&amp;gt;(TService oInstance);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* OR */&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TService Instance&amp;lt;TService&amp;gt; { get; set;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface ISomeService&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class Usage&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public Usage()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IServiceContainer oContainer = null;// new ServiceContainer();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; * Configure the container&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; * ...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; */&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ISomeService oService = oContainer.GetInstanceOf&amp;lt;ISomeService&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;oContainer.SetInstanceOf&amp;lt;ISomeService&amp;gt;(oService);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* OR */&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ISomeService oService = oContainer.InstanceOf&amp;lt;ISomeService&amp;gt;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;oContainer.InstanceOf&amp;lt;ISomeService&amp;gt; = oService;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
</description></item></channel></rss>