tag:blogger.com,1999:blog-5654876.post8643419985896094324..comments2024-01-07T00:15:48.561+02:00Comments on Marcel Popescu: Non-nullable reference types in C#Marcelhttp://www.blogger.com/profile/13951354231483245521noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-5654876.post-53612661131353722172011-01-10T16:14:11.392+02:002011-01-10T16:14:11.392+02:00Your implementation is open to this line of code:
...Your implementation is open to this line of code:<br /><br /><b><br />Test( new NotNull<string>() );<br /></b><br /><br />You're right to use a <b>readonly</b> field to store the reference however you should make it private and return the reference through a property that (additionally) checks for <b>null</b> before returning.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5654876.post-83629268478937855282009-07-03T17:40:20.508+03:002009-07-03T17:40:20.508+03:00It's dont assertion.
[Test]
pu...It's dont assertion.<br /> [Test]<br /> public void testOperatorBoolEquals()<br /> {<br /> NotNull string obj = "some text";<br /> NotNull string obj2 = "some text";<br /> Assert.IsTrue(obj == obj2);<br /> }<br /><br />it's will be work with this changes<br /><br /> public static bool operator ==(NotNull T left, NotNull T right)<br /> {<br /> return Equals(left.value, right.value);<br /> }<br /><br /> public static bool operator !=(NotNull T left, NotNull T right)<br /> {<br /> return !Equals(left.value, right.value);<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5654876.post-14525413092943002422008-11-05T16:32:00.000+02:002008-11-05T16:32:00.000+02:00I just realized that "null" is a valid N...I just realized that "null" is a valid NotNull<string> value. Thanks, I will change the code.Marcelhttps://www.blogger.com/profile/13951354231483245521noreply@blogger.comtag:blogger.com,1999:blog-5654876.post-43098444291042906782008-11-05T16:30:00.000+02:002008-11-05T16:30:00.000+02:00Thank you for your advice and my apologies for not...Thank you for your advice and my apologies for not replying earlier. I will have to test this, as it's not directly obvious to me why Test(null) only fails inside the method, but I will test it and correct if needed.Marcelhttps://www.blogger.com/profile/13951354231483245521noreply@blogger.comtag:blogger.com,1999:blog-5654876.post-14980257984803987522008-11-04T14:17:00.000+02:002008-11-04T14:17:00.000+02:00Convert it to a struct at least.otherwise you have...Convert it to a struct at least.<BR/><BR/>otherwise you have allowed a direct pass of null:<BR/><BR/> public void TestNotNull()<BR/> {<BR/> string s;<BR/><BR/> s = "something";<BR/> Test(s);<BR/><BR/> s = null;<BR/> Test(s); <- Fails on enter<BR/><BR/> Test(null); <- Fails inside method.<BR/> }<BR/><BR/>using a struct would make it fail up front as intented.Anonymousnoreply@blogger.com