Click here to Skip to main content
16,010,268 members
Home / Discussions / C#
   

C#

 
GeneralRe: StreamReader/StreamWriter Pin
George_George28-May-08 0:38
George_George28-May-08 0:38 
GeneralRe: StreamReader/StreamWriter Pin
#realJSOP20-May-08 4:12
professional#realJSOP20-May-08 4:12 
GeneralRe: StreamReader/StreamWriter Pin
Vikram A Punathambekar20-May-08 4:37
Vikram A Punathambekar20-May-08 4:37 
GeneralRe: StreamReader/StreamWriter Pin
George_George20-May-08 23:51
George_George20-May-08 23:51 
GeneralRe: StreamReader/StreamWriter Pin
George_George20-May-08 23:50
George_George20-May-08 23:50 
GeneralRe: StreamReader/StreamWriter Pin
S. Senthil Kumar21-May-08 2:17
S. Senthil Kumar21-May-08 2:17 
GeneralRe: StreamReader/StreamWriter Pin
#realJSOP21-May-08 23:51
professional#realJSOP21-May-08 23:51 
AnswerRe: StreamReader/StreamWriter Pin
Daniel Grunwald20-May-08 2:19
Daniel Grunwald20-May-08 2:19 
You never know when the GC will finalize the object. In a lot of cases, that's fine; but often it's important to know when an object is disposed.
In the case of a StreamWriter writing to a file, the file is left open until the StreamWriter is disposed. If you leave that to the GC, the file will be left open for an undefined amount of time. If your app (or another app) tries to write to that file again during that time, it will fail because the file is still in use.
Whenever you "lock" some object (file, TCP port, ...), you have to ensure that you unlock it as soon as possible, so you have to call Dispose.
Also, for some other objects there's a limit on the maximum number of objects that the GC doesn't know about (e.g. Brush/Pen allocate GDI objects, and there's a maximum of 10000 GDI objects per process). It might be possible that you hit the limit if you allocate too many such objects between two GC runs. Explicitly disposing brushes/pens solves that problem (although you already have a serious problem if you allocate that many objects; but you were more likely to run into this issue on Windows 98/ME which allowed much less GDI objects).

Also, there are objects that don't get garbage collected if you don't dispose them - e.g. if an object adds itself as a handler for an event on a long-lived object; it doesn't get garbage collected until the event handler is unregistered by the Dispose method.
GeneralRe: StreamReader/StreamWriter Pin
George_George20-May-08 2:28
George_George20-May-08 2:28 
GeneralRe: StreamReader/StreamWriter Pin
Vikram A Punathambekar20-May-08 3:07
Vikram A Punathambekar20-May-08 3:07 
GeneralRe: StreamReader/StreamWriter Pin
George_George20-May-08 3:15
George_George20-May-08 3:15 
GeneralRe: StreamReader/StreamWriter Pin
Vikram A Punathambekar20-May-08 4:43
Vikram A Punathambekar20-May-08 4:43 
GeneralRe: StreamReader/StreamWriter Pin
George_George20-May-08 23:52
George_George20-May-08 23:52 
GeneralRe: StreamReader/StreamWriter Pin
N a v a n e e t h20-May-08 3:09
N a v a n e e t h20-May-08 3:09 
GeneralRe: StreamReader/StreamWriter Pin
George_George20-May-08 3:13
George_George20-May-08 3:13 
Questionhandling browser events from windows application Pin
mihir raut20-May-08 1:37
mihir raut20-May-08 1:37 
Questioncalling C++ method from C# .net? Pin
cristi_alonso20-May-08 1:36
cristi_alonso20-May-08 1:36 
AnswerRe: calling C++ method from C# .net? Pin
Laddie20-May-08 2:56
Laddie20-May-08 2:56 
GeneralRe: calling C++ method from C# .net? Pin
cristi_alonso20-May-08 18:03
cristi_alonso20-May-08 18:03 
QuestionProblem with CustomEventHandling. Pin
hdv21220-May-08 1:13
hdv21220-May-08 1:13 
AnswerRe: Problem with CustomEventHandling. Pin
buchstaben20-May-08 1:28
buchstaben20-May-08 1:28 
GeneralRe: Problem with CustomEventHandling. Pin
hdv21220-May-08 2:20
hdv21220-May-08 2:20 
Questionbrowser component Pin
asma_panjabi20-May-08 0:42
asma_panjabi20-May-08 0:42 
GeneralRe: browser component Pin
buchstaben20-May-08 1:26
buchstaben20-May-08 1:26 
QuestionWhy the exception "Could not save; currently locked by another user" raise? Pin
nicolus19-May-08 23:56
nicolus19-May-08 23:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.