<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx</link><description>I suspect this has been done several times before, but on my way home this evening I considered what would be needed for the reverse of nullable value types - non-nullable reference types. I&amp;#39;ve had a play with an initial implementation in MiscUtil</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1662152</link><pubDate>Thu, 15 Jan 2009 14:17:41 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1662152</guid><dc:creator>Remco</dc:creator><description>&lt;p&gt;John,&lt;/p&gt;
&lt;p&gt;Nice example of dealing with input requirements.&lt;/p&gt;
&lt;p&gt;However, i think it suffers from one problem: the exceptions that get raised have an &amp;quot;unexpected&amp;quot; stack trace. It wil report the call site (technically) correct as:&lt;/p&gt;
&lt;p&gt;Value cannot be null.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at TestApp.NotNull`1..ctor(T value) in C:\Temp\NonNullable\TestApp\TestApp\NotNull.cs:line 16&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at TestApp.NotNull`1.op_Implicit(T value) in C:\Temp\NonNullable\TestApp\TestApp\NotNull.cs:line 37&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at TestApp.ReverseNotNullDemo.Run() in C:\Temp\NonNullable\TestApp\TestApp\ReverseNotNullDemo.cs:line 13&lt;/p&gt;
&lt;p&gt;However, the upper two lines in the stack trace only confuse the troubled location. The problem exists in the call at line 3. I would like to see:&lt;/p&gt;
&lt;p&gt;Value cannot be null.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; at TestApp.ReverseNotNullDemo.Run() in C:\Temp\NonNullable\TestApp\TestApp\ReverseNotNullDemo.cs:line 13&lt;/p&gt;
&lt;p&gt;Now, I&amp;#39;ve used Delphi in an earlier life, and there you could use:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;raise Exception.Create() at $address&lt;/p&gt;
&lt;p&gt;If you had enough information about the call stack, you could trigger the exception at the troubled location, instead of the reporting line.&lt;/p&gt;
&lt;p&gt;Do you think something like that is possible in C# / .Net?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1662152" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1656574</link><pubDate>Fri, 12 Dec 2008 06:32:21 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1656574</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Johannes: Sorry I didn&amp;#39;t see this reply before. I must have approved it but not had the time to look at it properly. Nice work!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1656574" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1651839</link><pubDate>Tue, 21 Oct 2008 18:32:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1651839</guid><dc:creator>Johannes Hansen</dc:creator><description>&lt;p&gt;Hi Jon,&lt;/p&gt;
&lt;p&gt;thanks for this great little utility class I&amp;#39;m sure it will help us avoid many common errors in our internal libraries in the future.&lt;/p&gt;
&lt;p&gt;There is a problem with your solution however... Since you are using a struct (which you should) you also get a default parameterless constructor that lets a user circumvent the null check in the other constructor. the problem is that you can&amp;#39;t redefine this default constructor via. c# code. So I took the liberty to make a similar implementation where I&amp;#39;ve modified the IL to throw exception if you call the default parameterless constructor, it has also been marked as obsolete (error=true) to have a better design time experience for the developer.&lt;/p&gt;
&lt;p&gt;You can get a copy from:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://cid-f61a12f6f7c6dd75.skydrive.live.com/self.aspx/Public/NonNull.zip"&gt;cid-f61a12f6f7c6dd75.skydrive.live.com/.../NonNull.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hope you like it.&lt;/p&gt;
&lt;p&gt;By the way, this is also being discussed on C9:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://channel9.msdn.com/forums/Coffeehouse/407089-Non-null-in-C/"&gt;channel9.msdn.com/.../407089-Non-null-in-C&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1651839" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1651464</link><pubDate>Tue, 21 Oct 2008 04:21:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1651464</guid><dc:creator>Vladimir Kelman</dc:creator><description>&lt;p&gt;John, could you explain in a bit more details why is it worse to specify it as&lt;/p&gt;
&lt;p&gt;public class NonNullable&amp;lt;T&amp;gt; where T : class {} &amp;nbsp;?&lt;/p&gt;
&lt;p&gt;To avoid referencing an original value we could use Object.MemberwiseClone() in a constructor:&lt;/p&gt;
&lt;p&gt;public NonNullable(T value) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if (value == null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;throw new ArgumentNullException(&amp;quot;value&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;this.value = value.MemberwiseClone();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This would make unnecessary if (value == null) check in a getter.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1651464" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1650916</link><pubDate>Wed, 15 Oct 2008 13:59:57 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650916</guid><dc:creator>Weeble</dc:creator><description>&lt;p&gt;Hehehe. I asked a very similar question on microsoft.public.dotnet.languages.csharp last year, but I didn&amp;#39;t really get much response. I&amp;#39;ve linked to the post if you click on my name. It&amp;#39;s frustrating that there really no good option at the moment, pick one of: masses of null-checking boilerplate code in all your methods; hard to debug NullReferenceExceptions; clever but awkward semi-kludges such as this.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650916" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1650012</link><pubDate>Tue, 07 Oct 2008 17:33:54 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650012</guid><dc:creator>Abraham Pinzur</dc:creator><description>&lt;p&gt;I created a similar struct type and have been experimenting with it for almost a year. I love the idea of a self-documenting mechanism for declaring method preconditions, class invariants, etc..&lt;/p&gt;
&lt;p&gt;In hopes of keeping the impact to surrounding code as small as possible, I (like you) made both conversion operators implicit. Unfortunately, without language support, I&amp;#39;ve still found myself forced to think far too frequently about conversions to and from NonNullable&amp;lt;T&amp;gt;. For instance:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;1. Accessing the methods (and properties) of a NonNullable object requires ubiquitous usage of the Value property or an explicit cast. (I quickly gave up on using this for fields and properties.)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;2. Casting from one NonNullable type to another is cumbersome. For instance, if class B derives from class A, then I should be able to substitute a NonNullable&amp;lt;B&amp;gt; wherever the code expects a NonNullable&amp;lt;A&amp;gt;. However, given C#&amp;#39;s lack of support for generic type covariance, I&amp;#39;m once again forced to use an explicit cast to class A.&lt;/p&gt;
&lt;p&gt;Perhaps someone has some suggestions for improving usability around a type like this?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650012" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1650005</link><pubDate>Tue, 07 Oct 2008 14:35:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650005</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Larry: Such is the downside of blogging in a relative hurry :)&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure that it&amp;#39;s feasible to introduce Spec# compile-time requirements at this point in time - it&amp;#39;s like introducing const-correctness into an existing library. Who&amp;#39;s going to go over all of your code to change all the signatures?&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t *think* any of this is likely to come in C# 4 - which is likely to be a relatively small set of enhancemenets anyway - but there&amp;#39;s certainly stuff the compiler could do to at least enforce constraints at execution time without as having to laboriously write out all the pre/post-condition checking manually. Maybe C# 5...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650005" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1650002</link><pubDate>Tue, 07 Oct 2008 14:26:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650002</guid><dc:creator>Larry Smith</dc:creator><description>&lt;p&gt;I&amp;#39;m surprised you didn&amp;#39;t mention Spec# or Sing#, both internal variations of C# created by Microsoft. (Singularity, Microsoft&amp;#39;s experimental operating system, is written in Sing#.) IIRC, they both allow you to write, say, &amp;lt;string! s = &amp;quot;foo&amp;quot;;&amp;gt;, where the &amp;quot;!&amp;quot; specifies that &amp;lt;s&amp;gt; is guaranteed to be non-null. With this declaration in hand, the compiler can (and, I think, does) ensure that nowhere throughout the entire application (including subroutine parameters (suitably declared with !&amp;#39;s themselves)) can&amp;#39;t be null, else you&amp;#39;ll get a compile-time error.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m hoping, verging on expecting, that this (and other Spec# features) will be part of C# 4.0.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650002" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1650001</link><pubDate>Tue, 07 Oct 2008 14:18:38 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650001</guid><dc:creator>Nicholas Blumhardt</dc:creator><description>&lt;p&gt;Neat. I&amp;#39;d call it Required&amp;lt;T&amp;gt; though :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650001" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649977</link><pubDate>Tue, 07 Oct 2008 09:46:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649977</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Drakiula:&lt;/p&gt;
&lt;p&gt;There&amp;#39;s still the problem of not being able to specify a parameterless constructor. Also, the constraint is quite a restrictive one and I suspect wouldn&amp;#39;t actually be what you wanted very often. It would also involve creating far too many instances of T for no reason!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649977" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649976</link><pubDate>Tue, 07 Oct 2008 09:41:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649976</guid><dc:creator>Drakiula</dc:creator><description>&lt;p&gt;Sorry again ...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649976" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649975</link><pubDate>Tue, 07 Oct 2008 09:39:51 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649975</guid><dc:creator>Drakiula</dc:creator><description>&lt;p&gt;Sorry for double posting, I wanted to post&lt;/p&gt;
&lt;p&gt;public NonNullable()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; this.value = new T();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Alas, for this to work, the new() generic constraint for T would have to be added.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649975" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649973</link><pubDate>Tue, 07 Oct 2008 09:28:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649973</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Drakiula:&lt;/p&gt;
&lt;p&gt;Firstly, you can&amp;#39;t add parameterless constructors to structs. Secondly, that would have no effect anyway, as it would just set value to null (as that&amp;#39;s the default value for all reference types).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649973" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649972</link><pubDate>Tue, 07 Oct 2008 09:22:47 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649972</guid><dc:creator>Drakiula</dc:creator><description>&lt;p&gt;How about adding&lt;/p&gt;
&lt;p&gt;public NonNullable()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;this.value = default(T);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649972" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649971</link><pubDate>Tue, 07 Oct 2008 08:59:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649971</guid><dc:creator>James Hart</dc:creator><description>&lt;p&gt;Neat. I think the problems you&amp;#39;ve listed - that default instances and values returned by the parameterless constructor are null values - make calling the type &amp;#39;NonNullable&amp;#39; a bit optimistic, however.&lt;/p&gt;
&lt;p&gt;But as a way of automating null-checking on a parameter, this is pretty neat, actually, thanks to the implicit conversion operators - has an aspect-oriented flavour to it. Maybe the name should be changed to be more appropriate as a type declaration in an argument list - NotNull&amp;lt;string&amp;gt; perhaps?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649971" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649965</link><pubDate>Tue, 07 Oct 2008 07:24:23 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649965</guid><dc:creator>Wedge</dc:creator><description>&lt;p&gt;I wonder how you might use this sort of thing to better facilitate the Null Object pattern. I can see how a static method in an interface might be worthwhile now. I suppose you could use reflection to hack in some kind of duck-typing in order to get at a static factory method that returned a null object reference for the type.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s a shame C# doesn&amp;#39;t have typedefs, done right this pattern could be really slick.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649965" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649955</link><pubDate>Tue, 07 Oct 2008 05:25:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649955</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Eddie/Jay: This is a struct. The default value (and the result of calling the parameterless constructor) will wrap a null reference.&lt;/p&gt;
&lt;p&gt;Elij: You can&amp;#39;t overload the equal operator - but the implicit conversion will make your code blow up appropriately.&lt;/p&gt;
&lt;p&gt;Jay: Even for trivial values, I prefer properties over public fields. The only time I ever expose public fields is for constants, and even then I&amp;#39;m somewhat leary.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649955" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649954</link><pubDate>Tue, 07 Oct 2008 05:10:47 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649954</guid><dc:creator>Jay Bazuzi</dc:creator><description>&lt;p&gt;I like it. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Two concerns:&lt;/p&gt;
&lt;p&gt;1. Why does the Value property bother checking for null &amp;amp; throwing? &amp;nbsp;Haven&amp;#39;t you already confirmed non-null in the constructor?&lt;/p&gt;
&lt;p&gt;2. If the implementation of Value is going to be trivial, why bother with a property at all? &amp;nbsp;I&amp;#39;d skip the property, make the field pubilc, and rename it to &amp;quot;Value&amp;quot;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649954" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649951</link><pubDate>Tue, 07 Oct 2008 03:32:13 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649951</guid><dc:creator>Elij</dc:creator><description>&lt;p&gt;Wouldn&amp;#39;t you override the &amp;quot;equal&amp;quot; operator so that &amp;quot;NonNullable&amp;lt;T&amp;gt; blah = null&amp;quot; also throws an exception?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649951" width="1" height="1"&gt;</description></item><item><title>re: Non-nullable reference types</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx#1649944</link><pubDate>Tue, 07 Oct 2008 00:22:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1649944</guid><dc:creator>Eddie Velasquez</dc:creator><description>&lt;p&gt;Isn&amp;#39;t the null check in the Value property getter redundant? The only way that value is set as far as I can see is through the constructor, which checks for null.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1649944" width="1" height="1"&gt;</description></item></channel></rss>