<?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>Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx</link><description>This is a follow-up to my 2005 post on the same subject which I believe sets a personal record for the longest time between parts of a series. (Of course, I didn't know it was a series when I started it.) Please read the previous article in this series</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#10077901</link><pubDate>Tue, 19 Oct 2010 15:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10077901</guid><dc:creator>Patrick</dc:creator><description>&lt;p&gt;I see potential threat of abusing this cross sibling calls to protected methods of the base class, but there&amp;#39;s also similar risk of malicious calls from malicious classes that derive from one of the siblings. I mention calls to methods but it also covers accessing other things like properties.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my example - some software development company structure. Where only people of development background are allowed to give work to developers. Others have to go to the manager to get it vetted.&lt;/p&gt;
&lt;p&gt;public class Employee {}&lt;/p&gt;
&lt;p&gt;public class Developer : Employee()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected void virtual Develop(Requirements data) { // develop }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class CSharpDeveloper : Developer&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected &amp;nbsp;void DoSomeCSharpStuff() {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; override void Develop(Requirements data) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; // implement all code changes, remove from &amp;#39;data&amp;#39; and delegate rest to other devs }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class DbDeveloper : Developer&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected void DoSomeDbStuff() {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; override void Develop(Requirements data) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;// implement all db changes, remove from &amp;#39;data&amp;#39; and delegate rest to other devs }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class DevelopmentManager : Developer, IAmManager&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; DbDeveloper [] dbDevs;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; CSharpDeveloper[] cDevs;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; override void Develop(Requirements data) { // are you kidding me?! find relevant developer and delegate work }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; IAmManager.RequestWork(Requirements data) { // assess whether relevant to the devs and allocate or bin it }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class Tester : Employee {}&lt;/p&gt;
&lt;p&gt;So if sibling calls were allowed then DevelopmentManager would be able to call Develop() on devs in his Team. Developers would be protected from any calls from Testers, they will have to go through proper channels :-)&lt;/p&gt;
&lt;p&gt;Now someone could derive malicious class from Developer class and send malicious calls to Develop() method, which relies on access modifier and doesn&amp;#39;t validate input as much.&lt;/p&gt;
&lt;p&gt;But &amp;nbsp;preventing cross sibling calls doesn&amp;#39;t stop malicious class deriving from CSharpDeveloper class and also invoking calls.&lt;/p&gt;
&lt;p&gt;&amp;#39;Internal protected&amp;#39; access modifier doesn&amp;#39;t solve the problem because it would limit this model to 1 assembly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10077901" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#10077900</link><pubDate>Tue, 19 Oct 2010 15:47:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10077900</guid><dc:creator>Patrick</dc:creator><description>&lt;p&gt;I see potential threat of abusing this cross sibling calls to protected methods of the base class, but there&amp;#39;s also similar risk of malicious calls from malicious classes that derive from one of the siblings. I mention calls to methods but it also covers accessing other things like properties.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my example - some software development company structure. Where only people of development background are allowed to give work to developers. Others have to go to the manager to get it vetted.&lt;/p&gt;
&lt;p&gt;public class Employee {}&lt;/p&gt;
&lt;p&gt;public class Developer : Employee()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected void virtual Develop(Requirements data) { // develop }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class CSharpDeveloper : Developer&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected &amp;nbsp;void DoSomeCSharpStuff() {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; override void Develop(Requirements data) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; // implement all code changes, remove from &amp;#39;data&amp;#39; and delegate rest to other devs }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class DbDeveloper : Developer&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; protected void DoSomeDbStuff() {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; override void Develop(Requirements data) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;// implement all db changes, remove from &amp;#39;data&amp;#39; and delegate rest to other devs }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class DevelopmentManager : Developer, IAmManager&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; DbDeveloper [] dbDevs;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; CSharpDeveloper[] cDevs;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; override void Develop(Requirements data) { // are you kidding me?! find relevant developer and delegate work }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; IAmManager.RequestWork(Requirements data) { // assess whether relevant to the devs and allocate or bin it }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class Tester : Employee {}&lt;/p&gt;
&lt;p&gt;So if sibling calls were allowed then DevelopmentManager would be able to call Develop() on devs in his Team. Developers would be protected from any calls from Testers, they will have to go through proper channels :-)&lt;/p&gt;
&lt;p&gt;Now someone could derive malicious class from Developer class and send malicious calls to Develop() method, which relies on access modifier and doesn&amp;#39;t validate input as much.&lt;/p&gt;
&lt;p&gt;But &amp;nbsp;preventing cross sibling calls doesn&amp;#39;t stop malicious class deriving from CSharpDeveloper class and also invoking calls.&lt;/p&gt;
&lt;p&gt;&amp;#39;Internal protected&amp;#39; access modifier doesn&amp;#39;t solve the problem because it would limit this model to 1 assembly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10077900" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#9860383</link><pubDate>Fri, 07 Aug 2009 19:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9860383</guid><dc:creator>Tom</dc:creator><description>&lt;p&gt;I would take the easy route and give each item a Bulk property and each container a MaxCapacity property to serve as a validation tool for determining which items fit inside which containers. &amp;nbsp;Since containers are items, they too follow this &amp;quot;bulk&amp;quot; policy — as such, a room won't fit inside a sack, but it could fit inside a house or a forest, if you were so inclined to use that level of abstraction. &amp;nbsp;(It also has the lovely side effect of providing an indicator for when a container is &amp;quot;full.&amp;quot;)&lt;/p&gt;
&lt;p&gt;You might also consider a component design approach, wherein items have components, and components (rather than the class hierarchy) define the item's functionality. &amp;nbsp;But I won't go into that. &amp;nbsp;If you're interested, here's a good link to get you started: &amp;nbsp;&lt;a rel="nofollow" target="_new" href="http://www.enchantedage.com/node/45"&gt;http://www.enchantedage.com/node/45&lt;/a&gt;. &amp;nbsp;The title says XNA, but the concept is very general.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9860383" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#9254929</link><pubDate>Sun, 28 Dec 2008 16:02:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9254929</guid><dc:creator>Alexey</dc:creator><description>&lt;p&gt;I wonder if this problem (maintaining bilateral references) can be solved the way you described if you add a third type: containers that aren't items. For example, a room can contain a box and a torch, a box can contain a sack and a a map, but nothing can can contain a room.&lt;/p&gt;
&lt;p&gt;Without multiple inheritance you have to extract the IContainer and IContainable interfaces and implement them in the abstract Item, Container and ContainerItem classes. Okay, you can put the Container class declaration into the Item class declaration, but what if Container is involved in another bilateral relationship?&lt;/p&gt;
&lt;p&gt;Can you user partial class declarations to grant access to specific internal members to a specific type?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9254929" width="1" height="1"&gt;</description></item><item><title>Protected Member Access, Part Four</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#8450994</link><pubDate>Fri, 02 May 2008 19:13:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8450994</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description>&lt;p&gt;In Part Two I asked a couple of follow-up questions, the first of which was: Suppose you were a hostile&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8450994" width="1" height="1"&gt;</description></item><item><title>Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#8422131</link><pubDate>Thu, 24 Apr 2008 19:46:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8422131</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description>&lt;p&gt;Holy goodness, I've been busy. The MVP Summit was fabulous for us; thanks to all who attended and gave&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8422131" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#8416825</link><pubDate>Tue, 22 Apr 2008 19:35:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8416825</guid><dc:creator>Weeble</dc:creator><description>&lt;p&gt;For the immutable question, how about introducing some sort of &amp;quot;view&amp;quot; classes? Items don't actually store upward links to their containers, but a container can create &amp;quot;views&amp;quot; of its children, which are immutable wrappers around the items that also have an upward link back to (a view of?) the container. Of course, it could get nasty making all the view classes.&lt;/p&gt;
&lt;p&gt;For that matter, what should be the return value of an immutable container's add or remove method? Suppose we have a bag inside a box, and we do &amp;quot;bag.Add(torch);&amp;quot;, what's the return value? Do we get a bag containing a torch? Do we get a bag that's inside a box, and which contains a torch? Do we get a box containing a bag containing a torch? Are we even allowed to do this?&lt;/p&gt;
&lt;p&gt;How do we call the remove method? If we're copying the hierarchy in order to make new versions, can we sensibly use a reference to identify the item we want to remove, or do we need to give our objects value semantics?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8416825" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#8399577</link><pubDate>Thu, 17 Apr 2008 00:16:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8399577</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;I noticed no one really addressed Eric's 2nd question about how to &amp;quot;make this hierarchy an immutable collection&amp;quot; &amp;nbsp;I could not really think of any good way to make the hierarchy immutable and have items know their parents without having to rebuild the entire hierarchy every time something changes.&lt;/p&gt;
&lt;p&gt;The only thing I could come up with to represent this type of immutible hierarchy was to have the container hold its child items in a binary tree structure.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8399577" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#8397983</link><pubDate>Tue, 15 Apr 2008 20:51:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8397983</guid><dc:creator>Duarte Cunha Leão</dc:creator><description>&lt;p&gt;I think that what I show next could be an &lt;/p&gt;
&lt;p&gt;elegant solution to the &amp;quot;friend class&amp;quot; design problem:&lt;/p&gt;
&lt;p&gt;namespace TwoMutuallyKnownClasses&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	public abstract class Item &lt;/p&gt;
&lt;p&gt;		//: BaseClass, BaseInterface &lt;/p&gt;
&lt;p&gt;		friends Bag&lt;/p&gt;
&lt;p&gt;		//where T : SomeType&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		private Item _parent;&lt;/p&gt;
&lt;p&gt;		public Item Parent&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			get { return _parent; }&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;	//	This method is accessible from derived classes (protected) and from friend classes (friends)&lt;/p&gt;
&lt;p&gt;		protected friends void SetParent(Item parent)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			_parent = parent;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	public abstract class Bag : Item&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		private List&amp;lt;Item&amp;gt; _list = new List&amp;lt;Item&amp;gt;();&lt;/p&gt;
&lt;p&gt;		public void Add(Item item)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			item.SetParent(this); // Not an error in C# 4.0&lt;/p&gt;
&lt;p&gt;			_list.Add(item);&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Something like this captures the special relationship &lt;/p&gt;
&lt;p&gt;between these two classes.&lt;/p&gt;
&lt;p&gt;The trust relationship that is expressed &lt;/p&gt;
&lt;p&gt;does not pervase to the containing assembly's types, &lt;/p&gt;
&lt;p&gt;by lack of expressiveness of the language. &lt;/p&gt;
&lt;p&gt;That would be the case if the &amp;#171;internal&amp;#187; keyword had to be used,&lt;/p&gt;
&lt;p&gt;in this place, to express the same real relationship.&lt;/p&gt;
&lt;p&gt;The number of types in an assembly can easily get very large, &lt;/p&gt;
&lt;p&gt;and it may not be feasible to assume that they all &lt;/p&gt;
&lt;p&gt;share the same level of trust, as the &amp;#171;internal&amp;#187; keyword obliges.&lt;/p&gt;
&lt;p&gt;Also it is common that several different people contribute to a given assembly,&lt;/p&gt;
&lt;p&gt;possibly at different points in time,&lt;/p&gt;
&lt;p&gt;with decreasing knowledge of the original author's intentions.&lt;/p&gt;
&lt;p&gt;Personally, I think code accessibility and intent expressiveness&lt;/p&gt;
&lt;p&gt;should not (need to) depend on assembly boundaries.&lt;/p&gt;
&lt;p&gt;Just like the &amp;#171;internal&amp;#187; keyword denotes all types of an assembly,&lt;/p&gt;
&lt;p&gt;the &amp;#171;friends&amp;#187; keyword would denote the set of types considered friends &lt;/p&gt;
&lt;p&gt;by a given class.&lt;/p&gt;
&lt;p&gt;Notice that the proposed expressiveness is somehow inverse&lt;/p&gt;
&lt;p&gt;of that of the C++ language, &lt;/p&gt;
&lt;p&gt;where a class &amp;#171;Item&amp;#187; says:&lt;/p&gt;
&lt;p&gt;&amp;quot;Method Bag.Add has access to me&amp;quot;&lt;/p&gt;
&lt;p&gt;or if Item trusts all of Bags methods:&lt;/p&gt;
&lt;p&gt;&amp;quot;Class Bag has access to me&amp;quot;.&lt;/p&gt;
&lt;p&gt;Instead, in a CIL language (&lt;a rel="nofollow" target="_new" href="http://en.wikipedia.org/wiki/Common_Intermediate_Language"&gt;http://en.wikipedia.org/wiki/Common_Intermediate_Language&lt;/a&gt;) &amp;nbsp;&lt;/p&gt;
&lt;p&gt;we would say:&lt;/p&gt;
&lt;p&gt;&amp;quot;My method SetParent may be accessed by (any method of) any of my friend classes&amp;quot;&lt;/p&gt;
&lt;p&gt;and that:&lt;/p&gt;
&lt;p&gt;&amp;quot;Class Bag is my friend&amp;quot;&lt;/p&gt;
&lt;p&gt;Would this be a good approach?&lt;/p&gt;
&lt;p&gt;Would it bring any problems with it?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8397983" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/03/28/why-can-t-i-access-a-protected-member-from-a-derived-class-part-two-why-can-i.aspx#8394548</link><pubDate>Mon, 14 Apr 2008 23:32:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8394548</guid><dc:creator>Eamon Nerbonne</dc:creator><description>&lt;p&gt;The option using &amp;quot;internal&amp;quot; looks reasonable to me - notice that both the &amp;quot;mananger&amp;quot; who consumes and generates must be aware of the classes it manages and that the &amp;quot;black-box&amp;quot; classes must be aware of the manager; that means you're very unlikely to be able to put the manager and the blackbox in separate assemblies anyhow due to circular dependancies. &amp;nbsp;Therefore, putting both in a single &amp;quot;trusted&amp;quot; assembly seems reasonable. &amp;nbsp;Of course, technically, the manager class might provide its encapsulation services to any sort of class (and thus breaking the circular dependancy), but I can't really imagine such a situation in practice...&lt;/p&gt;
&lt;p&gt;The following would be an alternative (but it's not pretty...)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class ManageP&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object key=new object();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static Func&amp;lt;P&amp;gt; trustedMaker;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void SetPMaker(P proof, Func&amp;lt;P&amp;gt; suggestedMaker) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (proof != null &amp;amp;&amp;amp; trustedMaker == null) trustedMaker = suggestedMaker;&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;static ManageP() { P.Initialize(); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main(string[] args) &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;foreach (var i in Enumerable.Range(0,10)) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.WriteLine(trustedMaker());&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;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public sealed class P&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static int count=0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private P() { count++; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override string ToString() {return &amp;quot;#&amp;quot; + count;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void Initialize() { ManageP.SetPMaker(new P(), () =&amp;gt; new P()); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8394548" width="1" height="1"&gt;</description></item></channel></rss>