<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" media="screen" href="/App_Themes/default/rss.xslt"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:evnet="http://www.mscommunities.com/rssmodule/"><channel><title>Comment Feed for Why can't you explicitly cast an int to a string in C#? (TechOff on Channel 9)</title><atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/forums/techoff/410397-why-cant-you-explicitly-cast-an-int-to-a-string-in-c/rss/default.aspx" /><image><url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url><title>Comment Feed for Why can't you explicitly cast an int to a string in C#? (TechOff on Channel 9)</title><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/</link></image><description>Why can't you explicitly cast an int to a string in C#?</description><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/</link><language>en-us</language><pubDate>Wed, 25 Jun 2008 17:35:33 GMT</pubDate><lastBuildDate>Wed, 25 Jun 2008 17:35:33 GMT</lastBuildDate><generator>EvNet (EvNet, Version=1.0.3608.3122, Culture=neutral, PublicKeyToken=null)</generator><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;BLOCKQUOTE&gt;1. The moment "internal details" are documented they stop being internal details. Simply stating in documentation that "Close calls Dispose" is not implementation detail. In fact the guideline for implementing Dispose actually states that, that if a Close method is implemented then it should just call Dispose(). &lt;/BLOCKQUOTE&gt;So if I document the fact that my class has a private Dictionary member in which I store all my stuff, then that private member is no longer an internal detail? That's pretty strange. 
&lt;BLOCKQUOTE&gt;2. All my comments/examples were about .NET Framework classes.&lt;/BLOCKQUOTE&gt;All my comments were about general good programming practice, not .NET specifically. 
&lt;BLOCKQUOTE&gt;Why would it drop all details Bas?&lt;/BLOCKQUOTE&gt;Performance? Who knows? That's not the point. The point is that you shouldn't rely on stuff that you know to be true but isn't actually part of the contract. 
&lt;BLOCKQUOTE&gt;by default- no classes in .net purposely transact their changes.. just for streams its important for control and performance that we have the ability to manage the flushing ourselves..&lt;/BLOCKQUOTE&gt;Sure, maybe it's all fine and dandy in .NET. What if you come across a third party library and they didn't implement it that way? 
&lt;BLOCKQUOTE&gt;If I'm done with an object, I should expect that object ensures my changes are done.. &lt;/BLOCKQUOTE&gt;No you shouldn't, unless it's stated in the contract. 
&lt;BLOCKQUOTE&gt;But again it comes back to my point, and as sven pointed out - .net classes DON'T act in that way.. so why should I do it unless theres a problem?&lt;/BLOCKQUOTE&gt;Good practice resulting in robust code, even if you eventually start using a third party class library that didn't interpret "release all resources" as "finish up, then release"? That'd still comply with IDisposable's contract.&amp;nbsp;&lt;BR&gt;&lt;BR&gt;Adhere to the &lt;EM&gt;contract&lt;/EM&gt;, not what you know about the &lt;EM&gt;implementation&lt;/EM&gt;. And since you shouldn't know anything about the implementation, just adhere to the contract and be safe. The implementation might radically change in a next version: that the whole point of abstraction and encapsulation.&lt;BR&gt;&lt;BR&gt;Anyway, evildictator is a lot better at conveying this than I am.&lt;BR&gt;&lt;BR&gt;Edit:&amp;nbsp;&lt;A href="http://weblogs.asp.net/pwilson/archive/2004/02/20/77435.aspx"&gt;Paul Wilson&lt;/A&gt; does a pretty good job too.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411856</link><pubDate>Wed, 25 Jun 2008 17:27:16 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411856</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411856/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>1. The moment "internal details" are documented they stop being internal details. Simply stating in documentation that "Close calls Dispose" is not implementation detail. In fact the guideline for implementing Dispose actually states that, that if a Close method is implemented then it should just&amp;#8230;</evnet:previewtext><dc:creator>Bas</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411856/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>If a class chooses to do something as part of a method that is not defined by the contract's method that's up to it. But you shouldn't rely (in general) on IDisposable doing anything more than disposing of unmanaged resources and memory which is what implementing IDisposable's contract means.&lt;br&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411825</link><pubDate>Wed, 25 Jun 2008 14:30:22 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411825</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411825/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>If a class chooses to do something as part of a method that is not defined by the contract's method that's up to it. But you shouldn't rely (in general) on IDisposable doing anything more than disposing of unmanaged resources and memory which is what implementing IDisposable's contract means.</evnet:previewtext><dc:creator>evildictaitor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411825/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Depends on what you really mean by "contract" in this case. In any case:&lt;BR&gt;&lt;BR&gt;- .NET/C# interfaces are a weak form of contracts. All they do is require a class to implement some methods, everything else is left to the documentation.&lt;BR&gt;- There's nothing stopping an implementation to exceed contract requirements.&lt;BR&gt;- Stream's IDisposable implementation exceeds contract requirements and documents that thus becoming a contract in itself because that's how contracts are defined in .NET/C#.&lt;BR&gt;- Contract or not, the fact that Stream.Dispose writes changes to the store was documented and no matter how the internal implementation changs this must remain true.&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;And since _stevo mentioned transactions: the TransactionScope is a perfect example of exceeding IDisposable's "contract". It does the opposite of Stream, it rollbacks any changes that were made. Not only it does that but that's the only way to rollback a transaction because there's no Rollback method provided. Even more, TransactionScope.Dispose documentation does not mention anything about realising managed and unmanaged resources.&lt;BR&gt;&lt;BR&gt;I guess TransactionScope's creation was the moment when IDisposable silently converted to a general purpose interface beyond its "release managed and unmanaged resources" original intent. Because this interface was used by the using C# keyword it is now used to model scope based lifetime similar to C++. Now this is not strictly related to "to Close or not to Close" but about what an "IDisposable&amp;nbsp;contract" can really mean.&lt;BR&gt;&lt;BR&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411809</link><pubDate>Wed, 25 Jun 2008 13:26:04 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411809</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411809/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Depends on what you really mean by "contract" in this case. In any case:- .NET/C# interfaces are a weak form of contracts. All they do is require a class to implement some methods, everything else is left to the documentation.- There's nothing stopping an implementation to exceed contract&amp;#8230;</evnet:previewtext><dc:creator>Dexter</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411809/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Because there's no guarrantee that they will continue to do so in future.&lt;br /&gt;
&lt;br /&gt;
You should program to &lt;em&gt;contracts &lt;/em&gt;not to &lt;em&gt;implementations&lt;/em&gt;.&lt;br /&gt;
&lt;br /&gt;
According to IDisposable:&lt;br /&gt;
&lt;br /&gt;

&lt;table cellspacing="0" cellpadding="0"&gt;
    
        &lt;tr&gt;
            &lt;td colspan="2"&gt;
            &lt;pre&gt;/// &amp;lt;summary&amp;gt;Defines a method to release allocated unmanaged resources.&amp;lt;/summary&amp;gt;
/// &amp;lt;filterpriority&amp;gt;2&amp;lt;/filterpriority&amp;gt;
[&lt;a title="System.Runtime.InteropServices.ComVisibleAttribute.ComVisibleAttribute(bool visibility);"&gt;ComVisible&lt;/a&gt;(true)]
public interface &lt;strong&gt;&lt;a&gt;IDisposable&lt;/a&gt;&lt;/strong&gt;
{
    /// &amp;lt;summary&amp;gt;Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.&amp;lt;/summary&amp;gt;
    /// &amp;lt;filterpriority&amp;gt;2&amp;lt;/filterpriority&amp;gt;
    &lt;a title="System.Void"&gt;void&lt;/a&gt; &lt;strong&gt;&lt;a&gt;Dispose&lt;/a&gt;&lt;/strong&gt;();
}
&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    
&lt;/table&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
    
        &lt;tr&gt;
            &lt;td colspan="2"&gt;
            &lt;pre&gt;&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    
&lt;/table&gt;
&lt;br /&gt;
That is to say that my hypothetical example &lt;em&gt;correctly implements IDisposable&lt;/em&gt; and so arguments that it is a silly example and so on aside, if you rely on implementation semantics from the .NET library, these may change without warning during a "bug fix" or a version change.&lt;br /&gt;
&lt;br /&gt;
I cannot emphasise this enough. &lt;em&gt;IDisposable does NOT guarrantee that it will flush buffers or correctly clean up the object - merely that it will free all unmanaged resources&lt;/em&gt; &lt;em&gt;to avoid resource and memory leaks. That's what it's for.&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
While file-streams are perhaps not a very good example, take some better example of some TcpConnection that it used for some persistent connection (e.g. for a MMO)&lt;br /&gt;
&lt;br /&gt;
class NetworkConnection : IDisposable {&lt;br /&gt;
&amp;nbsp; TcpClient client;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public void Logout();&lt;br /&gt;
&amp;nbsp; public void Close();&lt;br /&gt;
&amp;nbsp; public void Dispose();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Now when I call Close(), by the contract of what Close() does on objects, it should close the object as best it can, so in this case it should call Logout(). Let us say for example that Logout() uses the underlying connection to actively logout. The question I now pose to you is should Dispose() logout?&lt;br /&gt;
&lt;br /&gt;
My answer to this would be a resounding no. Dispose should guarrantee that it closes the client and that it releases the resources associated with the network layer and any buffers it may still have, but Dispose() does not have to guarrantee that it cleans up nicely, nor would it be appropriate in this case, since Dispose() could legitimately be called if the underlying client breaks for some reason. The Dispose() method of the NetworkConnection should merely be responsible for the cleaning up of resources associated with an object to avoid leaks, as the contractual obligation to &lt;em&gt;IDisposable &lt;/em&gt;itself says. &lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411751</link><pubDate>Wed, 25 Jun 2008 10:59:09 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411751</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411751/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Because there's no guarrantee that they will continue to do so in future.

You should program to contracts not to implementations.

According to IDisposable:



    
        
            
            /// &amp;lt;summary&amp;gt;Defines a method to release allocated unmanaged resources.&amp;lt;/summary&amp;gt;
///&amp;#8230;</evnet:previewtext><dc:creator>evildictaitor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411751/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Why would it drop all details Bas? by default- no classes in .net purposely transact their changes.. just for streams its important for control and performance that we have the ability to manage the flushing ourselves..&lt;BR&gt;&lt;BR&gt;If I'm done with an object, I should expect that object ensures my changes are done.. because I've worked with that object as if the changes have been done.. I'm not aware as a developer that the object works in a transacted mannor.&lt;BR&gt;&lt;BR&gt;But again it comes back to my point, and as sven pointed out - .net classes DON'T act in that way.. so why should I do it unless theres a problem?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411745</link><pubDate>Wed, 25 Jun 2008 10:37:57 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411745</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411745/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Why would it drop all details Bas? by default- no classes in .net purposely transact their changes.. just for streams its important for control and performance that we have the ability to manage the flushing ourselves..If I'm done with an object, I should expect that object ensures my changes are&amp;#8230;</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411745/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>1. The moment "internal details" are documented they stop being internal details. Simply stating in documentation that "Close calls Dispose" is not implementation detail. In fact the&amp;nbsp; guideline for implementing Dispose actually states that, that if a Close method&amp;nbsp;is implemented then it&amp;nbsp;should just call Dispose().&lt;BR&gt;&lt;BR&gt;2. All my comments/examples were about .NET Framework classes. If you have some crappy third party library where Dispose does not imply Close then of course it is ok/required to call Close. Hopefully such a lame library does not exist.&lt;BR&gt;&lt;BR&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411741</link><pubDate>Wed, 25 Jun 2008 10:17:06 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411741</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411741/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>1. The moment "internal details" are documented they stop being internal details. Simply stating in documentation that "Close calls Dispose" is not implementation detail. In fact the&amp;nbsp; guideline for implementing Dispose actually states that, that if a Close method&amp;nbsp;is implemented then&amp;#8230;</evnet:previewtext><dc:creator>Dexter</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411741/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>So does "releasing all managed and unmanaged resources in a clean and predictable way" mean "I'm gonna finish all this stuff you were doing and then release all resources" or does it mean "I'm gonna drop everything that was going on and release all resources"? &lt;BR&gt;The contract doesn't state that. It only states that resources should be released when Dispose() returns.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411723</link><pubDate>Wed, 25 Jun 2008 08:39:15 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411723</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411723/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>So does "releasing all managed and unmanaged resources in a clean and predictable way" mean "I'm gonna finish all this stuff you were doing and then release all resources" or does it mean "I'm gonna drop everything that was going on and release all resources"? The contract doesn't state that. It&amp;#8230;</evnet:previewtext><dc:creator>Bas</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411723/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>IDisposable was designed for this: to release all managed and unmanaged resources in a clean and predictable way. If a class that implements IDisposable does not work like this, then that's a bug with that class. A contract only works if the implementor does what you expect it to do.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411718</link><pubDate>Wed, 25 Jun 2008 08:15:56 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411718</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411718/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>IDisposable was designed for this: to release all managed and unmanaged resources in a clean and predictable way. If a class that implements IDisposable does not work like this, then that's a bug with that class. A contract only works if the implementor does what you expect it to do.</evnet:previewtext><dc:creator>Tommy Carlier</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411718/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>"Why in the world it is better practice to do something that the documentation states that's not needed is beyond me."&lt;BR&gt;&lt;BR&gt;Let's say I'm using some third party class library and the documentation carelessly states all sorts of details about the class's private&amp;nbsp;implementation. Should I write my code based on my knowledge of how those classes are implemented?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411693</link><pubDate>Wed, 25 Jun 2008 07:44:29 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411693</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411693/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>"Why in the world it is better practice to do something that the documentation states that's not needed is beyond me."Let's say I'm using some third party class library and the documentation carelessly states all sorts of details about the class's private&amp;nbsp;implementation. Should I write my code&amp;#8230;</evnet:previewtext><dc:creator>Bas</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411693/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;P&gt;Why in the world it is better practice to do something that the documentation states that's not needed is beyond me. That's really at the borderline of &lt;A href="http://en.wikipedia.org/wiki/Cargo_cult_programming"&gt;Cargo cult programming&lt;/A&gt;, calling methods for the sake of it. &lt;BR&gt;&lt;BR&gt;And I don't see how can it be less of a hassle to write x.Close() a thousand times instead of just checking the documentation once. It's not like there are a thousand classes that have both Close and Dispose and anyway you're supposed to read the documentation, that's why it exists.&lt;BR&gt;&lt;BR&gt;I give up!&lt;BR&gt;&lt;BR&gt;PS: Luckily no one here pretends that one must also call Flush before Close. I've seen cases like that and I tend to go kboom :)&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411680</link><pubDate>Wed, 25 Jun 2008 07:08:39 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411680</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411680/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Why in the world it is better practice to do something that the documentation states that's not needed is beyond me. That's really at the borderline of Cargo cult programming, calling methods for the sake of it. And I don't see how can it be less of a hassle to write x.Close() a thousand times&amp;#8230;</evnet:previewtext><dc:creator>Dexter</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411680/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Hypothetical examples aside, fact is that no framework class actually does that, and in most cases this is stated in the documentation (so relying on it is not relying on implementation details).&lt;BR&gt;&lt;BR&gt;Quite frankly I think you'd have to be pretty stupid as a library programmer to make a class behave like that, as it goes&amp;nbsp;against what programmers expect (even the C++ stream classes will properly flush in their destructors if you don't explicitly call close). You'd better have a damn good reason to do something like that.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411628</link><pubDate>Wed, 25 Jun 2008 01:17:40 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411628</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411628/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Hypothetical examples aside, fact is that no framework class actually does that, and in most cases this is stated in the documentation (so relying on it is not relying on implementation details).Quite frankly I think you'd have to be pretty stupid as a library programmer to make a class behave like&amp;#8230;</evnet:previewtext><dc:creator>Sven Groot</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411628/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>A class implements IDisposable correctly if a class does not leak memory or system resources if it's Dispose method is called, and it does so deterministically. Consider some hypotethical class:&lt;br&gt;&lt;br&gt;BufferedStream : Stream, IDisposable {&lt;br&gt;&amp;nbsp; string someBuffer;&lt;br&gt;&amp;nbsp; Stream underlyingStream;&lt;br&gt;&amp;nbsp; public BufferedStream(Stream underlyingStream){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.underlyingStream = underlyingStream;&lt;br&gt;&amp;nbsp; }&lt;br&gt;&amp;nbsp; //&amp;lt;summary&amp;gt;&lt;br&gt;&amp;nbsp; // Flushes the buffer to the stream&lt;br&gt;&amp;nbsp; //&amp;lt;/summary&amp;gt;&lt;br&gt;&amp;nbsp; public void Flush(){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(someBuffer != null) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; byte[] bytes =Encoding.UnicodeEncoding.GetBytes();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; underlyingStream.Write(bytes,0,bytes.Length);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; someBuffer = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp; }&lt;br&gt;&amp;nbsp; // &amp;lt;summary&amp;gt;&lt;br&gt;&amp;nbsp; // Writes a string to the stream&lt;br&gt;&amp;nbsp; // &amp;lt;/summary&amp;gt;&lt;br&gt;&amp;nbsp; public void Write(string str){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Flush(); someBuffer = str;&lt;br&gt;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp; // &amp;lt;summary&amp;gt;&lt;br&gt;&amp;nbsp; // Closes the stream&lt;br&gt;&amp;nbsp; // &amp;lt;/summary&amp;gt;&lt;br&gt;&amp;nbsp; public void Close(){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Flush();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; underlyingStream.Close();&lt;br&gt;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp; // &amp;lt;summary&amp;gt;&lt;br&gt;&amp;nbsp; // Disposes of system resources and native memory&lt;br&gt;&amp;nbsp; // &amp;lt;/summary&amp;gt;&lt;br&gt;&amp;nbsp; public void Dispose(){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; stream.Dispose();&lt;br&gt;&amp;nbsp; }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;i&gt;Now consider the following examples:&lt;/i&gt;&lt;br&gt;&lt;br&gt;public void Example1(){&lt;br&gt;&amp;nbsp; BufferedStream s = new BufferedStream(System.IO.File.OpenWrite("myfile.txt")));&lt;br&gt;&amp;nbsp; s.Write("Hello World!");&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;i&gt;This is bad because the unmanaged resources are potentially lost and the connection may never be closed, and will be in an inconsistent state when it does&lt;/i&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;public void Example2(){&lt;br&gt;&amp;nbsp; BufferedStream s = new BufferedStream(System.IO.File.OpenWrite("myfile.txt")));&lt;br&gt;
&amp;nbsp; s.Write("Hello World!");&lt;br&gt;&amp;nbsp; s.Dispose();&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;i&gt;This is bad because although no resource leaks occur (as guarranteed by IDisposable) there is no guarrantee that the underlying buffers will be flushed to the device, as this is not guarranteed by IDisposable&lt;br&gt;&lt;br&gt;&lt;/i&gt;public void Example3(){&lt;br&gt;
&amp;nbsp; BufferedStream s = new BufferedStream(System.IO.File.OpenWrite("myfile.txt")));&lt;br&gt;

&amp;nbsp; s.Write("Hello World!");&lt;br&gt;
&amp;nbsp; s.Close();&lt;br&gt;
}&lt;br&gt;&lt;br&gt;&lt;i&gt;This is bad because although Close guarrantees that the open object is closed, it makes no guarrantee about disposing of OS resources or memory&lt;/i&gt;&lt;br&gt;&lt;br&gt;public void Example4(){&lt;br&gt;

&amp;nbsp; BufferedStream s = new BufferedStream(System.IO.File.OpenWrite("myfile.txt")));&lt;br&gt;


&amp;nbsp; s.Write("Hello World!");&lt;br&gt;&amp;nbsp; s.Dispose();&lt;br&gt;

&amp;nbsp; s.Close();&lt;br&gt;

}&lt;br&gt;&lt;br&gt;&lt;i&gt;This is bad because Dispose() clears up the OS resources and thus any subsequent operations will likely break. Dispose() should always be the last thing you call on a method:&lt;/i&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
public void Example5(){&lt;br&gt;


&amp;nbsp; BufferedStream s = new BufferedStream(System.IO.File.OpenWrite("myfile.txt")));&lt;br&gt;



&amp;nbsp; s.Write("Hello World!");&lt;br&gt;


&amp;nbsp; s.Close();&lt;br&gt;
&amp;nbsp; s.Dispose();&lt;br&gt;


}&lt;br&gt;&lt;br&gt;&lt;i&gt;This is the only correct example that would lead to correct determinsitic behaviour for the BufferedStream&lt;/i&gt;. &lt;br&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411607</link><pubDate>Tue, 24 Jun 2008 22:56:43 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411607</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411607/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>A class implements IDisposable correctly if a class does not leak memory or system resources if it's Dispose method is called, and it does so deterministically. Consider some hypotethical class:BufferedStream : Stream, IDisposable {&amp;nbsp; string someBuffer;&amp;nbsp; Stream underlyingStream;&amp;nbsp;&amp;#8230;</evnet:previewtext><dc:creator>evildictaitor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411607/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>I agree, I shouldn't of referred to close as destruction.. but to be honest, I don't think&amp;nbsp;I could articulate my thoughts on this over this medium. I still keep to the same overall thoughts on this though.. unless theres a problem, why do it?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411597</link><pubDate>Tue, 24 Jun 2008 21:53:56 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411597</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411597/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>I agree, I shouldn't of referred to close as destruction.. but to be honest, I don't think&amp;nbsp;I could articulate my thoughts on this over this medium. I still keep to the same overall thoughts on this though.. unless theres a problem, why do it?</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411597/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Its not rude littleguru, its humour.. bloody is traditionally thought of as a brit word, along with having bad teeth and various other points. If anything, I'm digging at myself.&lt;BR&gt;&lt;BR&gt;Still, my point lies- programmers love to argue about things that generally make little difference.. I didn't say it was a bad thing, actually I find it amusing and I imagine can even be helpful at times.&lt;BR&gt;&lt;BR&gt;Perhaps I should append my posts with a 'tongue out' smiley, but we don't seem to have those yet.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411595</link><pubDate>Tue, 24 Jun 2008 21:44:33 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411595</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411595/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Its not rude littleguru, its humour.. bloody is traditionally thought of as a brit word, along with having bad teeth and various other points. If anything, I'm digging at myself.Still, my point lies- programmers love to argue about things that generally make little difference.. I didn't say it was a&amp;#8230;</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411595/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;BLOCKQUOTE&gt;Bloody programmers and your arguing about details..&lt;/BLOCKQUOTE&gt;Offtopic: Why are you so rude?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411589</link><pubDate>Tue, 24 Jun 2008 21:31:12 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411589</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411589/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Bloody programmers and your arguing about details..Offtopic: Why are you so rude?</evnet:previewtext><dc:creator>Christian Liensberger</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411589/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>No, not at all.. I'm very aware of the reasons disposable was intended.. I just don't see why you could ALSO call "close" without reason.. sure - if theres a class that doesn't implement idisposable correctly- then yea.. but why otherwise?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411586</link><pubDate>Tue, 24 Jun 2008 20:59:35 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411586</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411586/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>No, not at all.. I'm very aware of the reasons disposable was intended.. I just don't see why you could ALSO call "close" without reason.. sure - if theres a class that doesn't implement idisposable correctly- then yea.. but why otherwise?</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411586/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Close() isn't the destructor. And it's not about calling Close() because dispose "might not be implemented properly", it's about calling Close() because you shouldn't rely on the knowledge that Dispose() calls Close() itself.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411581</link><pubDate>Tue, 24 Jun 2008 20:56:50 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411581</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411581/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Close() isn't the destructor. And it's not about calling Close() because dispose "might not be implemented properly", it's about calling Close() because you shouldn't rely on the knowledge that Dispose() calls Close() itself.</evnet:previewtext><dc:creator>Bas</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411581/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Have you ever looked at the connections log on the database?&amp;nbsp; &lt;br&gt;&lt;br&gt;Failure to use close leaves connections open until the database "snipes" the connection.&amp;nbsp; It is better, from a resource perspective, to close and dispose of your connection.&amp;nbsp; &lt;br&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411574</link><pubDate>Tue, 24 Jun 2008 20:27:47 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411574</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411574/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Have you ever looked at the connections log on the database?&amp;nbsp; Failure to use close leaves connections open until the database "snipes" the connection.&amp;nbsp; It is better, from a resource perspective, to close and dispose of your connection.&amp;nbsp; </evnet:previewtext><dc:creator>lensman</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411574/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>You mean why call Dispose() or Close() at all?&lt;br&gt;&lt;br&gt;class SomeClass {&lt;br&gt;&lt;br&gt;&amp;nbsp; byte[] payload = Encoding.UnicodeEncoding.GetBytes("Hello World"));&lt;br&gt;&lt;br&gt;&amp;nbsp; SomeClass(){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo1();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo2();&lt;br&gt;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp; void Foo1(){&lt;br&gt;&amp;nbsp; &amp;nbsp; System.IO.FileStream fs = System.IO.File.Open("myFile.txt");&lt;br&gt;&amp;nbsp; &amp;nbsp; fs.Write(payload, 0, payload.Length);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // and don't release fs here - the GC will clean it up right?&lt;br&gt;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp; void Foo2(){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // let's hope the GC has cleaned up the old fs, if it hasn't this will break:&lt;br&gt;&amp;nbsp; &amp;nbsp; System.IO.FileStream fs = System.IO.File.Open("myFile.txt");&lt;br&gt;&amp;nbsp; &amp;nbsp; fs.Write(payload, 0, payload.Length);&lt;br&gt;&amp;nbsp; }&lt;br&gt;&lt;br&gt;}&lt;br&gt;&lt;br&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411567</link><pubDate>Tue, 24 Jun 2008 19:46:50 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411567</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411567/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>You mean why call Dispose() or Close() at all?class SomeClass {&amp;nbsp; byte[] payload = Encoding.UnicodeEncoding.GetBytes("Hello World"));&amp;nbsp; SomeClass(){&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo1();&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo2();&amp;nbsp; }&amp;nbsp; void Foo1(){&amp;nbsp; &amp;nbsp; System.IO.FileStream fs =&amp;#8230;</evnet:previewtext><dc:creator>evildictaitor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411567/Trackback.aspx</trackback:ping></item><item><title>Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;P&gt;Bloody programmers and your arguing about details.. that said.. I'm in the camp of not explicitely calling a 'destruction' method on the object if possible.. &lt;BR&gt;&lt;BR&gt;I won't post an essay repeating mostly what others have said, but I'll add that- why call the objects destruction method explicitely on the whim that you don't trust it to have implemented disposable properly..&lt;BR&gt;&lt;BR&gt;Unless theres a problem, why do it?&lt;/P&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411557</link><pubDate>Tue, 24 Jun 2008 18:57:42 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411557</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411557/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Bloody programmers and your arguing about details.. that said.. I'm in the camp of not explicitely calling a 'destruction' method on the object if possible.. I won't post an essay repeating mostly what others have said, but I'll add that- why call the objects destruction method explicitely on the&amp;#8230;</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411557/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>Does it imply, or does it guarantee close, and the exact behaviour of close?&lt;BR&gt;&lt;BR&gt;&amp;nbsp;Even if the documentation states "I'm just calling Close for you", I think it's better practice to explicitly call it yourself. Both because I find it less of a hassle to just call Close and be certain than check what, exactly, the Dispose method for every class I use does.&lt;BR&gt;&lt;BR&gt;And, like I said, I don't care how a Class's method is implemented, that's what abstraction is for. All I know is that Dispose releases resources. Wether it just drops everything it is doing and releases or if it helps me out and finishes stuff up for me in case I didn't is irrelevant to me.&lt;BR&gt;&lt;BR&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411550</link><pubDate>Tue, 24 Jun 2008 18:07:55 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411550</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411550/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Does it imply, or does it guarantee close, and the exact behaviour of close?&amp;nbsp;Even if the documentation states "I'm just calling Close for you", I think it's better practice to explicitly call it yourself. Both because I find it less of a hassle to just call Close and be certain than check what,&amp;#8230;</evnet:previewtext><dc:creator>Bas</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411550/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;P&gt;And what does Stream.Dispose&amp;nbsp;documentation say to us?&lt;BR&gt;&lt;BR&gt;"This method disposes the stream, by writing any changes to the backing store and closing the stream to release resources."&lt;BR&gt;&lt;BR&gt;I'd say it's pretty clear about flushing buffers...&lt;BR&gt;&lt;BR&gt;And I did not claim that they are semantically equivalent. Dispose implies Close, Close does not always imply Dispose, this is not equivalence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And this topic should be in a separate thread&amp;nbsp; :)&lt;/P&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411410</link><pubDate>Tue, 24 Jun 2008 10:05:23 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411410</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411410/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>And what does Stream.Dispose&amp;nbsp;documentation say to us?"This method disposes the stream, by writing any changes to the backing store and closing the stream to release resources."I'd say it's pretty clear about flushing buffers...And I did not claim that they are semantically equivalent. Dispose&amp;#8230;</evnet:previewtext><dc:creator>Dexter</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411410/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;A href="http://blogs.msdn.com/kimhamil/archive/2008/03/15/the-often-non-difference-between-close-and-dispose.aspx"&gt;The (often non-) difference between Close and Dispose&lt;/A&gt;.
&lt;BLOCKQUOTE&gt;Around Whidbey, it was realized that the rules around Dispose needed to be tightened up. C++ destructors -- which are deterministic -- needed to map to Dispose (the general Framework solution for &lt;B&gt;deterministic cleanup&lt;/B&gt;).&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;Based on this experience, I'd encourage you to think very carefully before attempting to use a domain-specific name instead of Dispose -- we're now at a place where users are familiar with Dispose and it is the most discoverable method name.&lt;/BLOCKQUOTE&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411402</link><pubDate>Tue, 24 Jun 2008 09:19:48 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411402</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411402/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>The (often non-) difference between Close and Dispose.
Around Whidbey, it was realized that the rules around Dispose needed to be tightened up. C++ destructors -- which are deterministic -- needed to map to Dispose (the general Framework solution for deterministic cleanup).
Based on this&amp;#8230;</evnet:previewtext><dc:creator>Tommy Carlier</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411402/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>While that may be true, there is a semantic difference between Close() and Dispose():&lt;br&gt;&lt;br&gt;Dispose() tells the object to dispose of its unmanaged resources properly and that it should prepare itself for cleanup.&lt;br&gt;&lt;br&gt;Close() tells the object that the actions being applied to it have now finished and that any existing buffers to the object should be flushed to the device.&lt;br&gt;&lt;br&gt;This means that you should (in general) call &lt;i&gt;both&lt;/i&gt;, although as Tommy said it's typically better practise to abstract the Dispose() method via the &lt;i&gt;using &lt;/i&gt;directive:&lt;br&gt;&lt;br&gt;using(MyDisposableObject obj = GetDisposableObject()){&lt;br&gt;&amp;nbsp; var result = DoSomething(obj);&lt;br&gt;&amp;nbsp; obj.Close();&lt;br&gt;}&lt;br&gt;&lt;br&gt;Note that using the &lt;i&gt;using &lt;/i&gt;statement guarrantees Dispose() via the try..finally directive, so the code above Closes (which properly flushes the internal buffers to the device) conditional on DoSomething() not throwing an exception, whereas Dispose() is unconditionally called (which is why the &lt;i&gt;using &lt;/i&gt;directive is preferrable).</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411399</link><pubDate>Tue, 24 Jun 2008 09:05:48 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411399</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411399/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>While that may be true, there is a semantic difference between Close() and Dispose():Dispose() tells the object to dispose of its unmanaged resources properly and that it should prepare itself for cleanup.Close() tells the object that the actions being applied to it have now finished and that any&amp;#8230;</evnet:previewtext><dc:creator>evildictaitor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411399/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Why can't you explicitly cast an int to a string in C#?</title><description>&lt;P&gt;In addition: indeed, I'm not aware of any case were calling Dispose won't have the same effect as calling Close. It is the opposite that doesn't always hold true, for example calling Close on a modal form does not dispose it.&lt;BR&gt;&lt;BR&gt;In general I don't like calling both Close and Dispose. I know nothing bad happens if you do that but I'm used to C++ where a double delete is an error and I consider it to be a bug when people only call Close and they don't put it in a finally block.&lt;BR&gt;&lt;/P&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411390</link><pubDate>Tue, 24 Jun 2008 07:56:46 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/410397-Why-cant-you-explicitly-cast-an-int-to-a-string-in-C/?CommentID=411390</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/411390/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>In addition: indeed, I'm not aware of any case were calling Dispose won't have the same effect as calling Close. It is the opposite that doesn't always hold true, for example calling Close on a modal form does not dispose it.In general I don't like calling both Close and Dispose. I know nothing bad&amp;#8230;</evnet:previewtext><dc:creator>Dexter</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/411390/Trackback.aspx</trackback:ping></item></channel></rss>