|
It wraps FileStream and provides dispose pattern to dispose FileStream instance. In that example, they have initialized FileStream, but not disposing after use. When you call Dispose() on a HasDisposeMethod instance, FileStream instance will get disposed. If HasDisposeMethod was like the following, no need of IDisposable implementation.
public class HasDisposeMethod
{
public HasDisposeMethod()
{
}
public void UseFileStream()
{
using(FileStream newFile = new FileStream(@"c:\temp.txt", FileMode.Open))
{
}
}
} In the above example, FileStream instance is used and disposed after user. So no need to provide dispose interface.
|
|
|
|
|
Thanks N a v a n e e t h,
Great reply! One further question, for the original sample in the MSDN page, if it does not implement IDisposable, what are the issues?
regards,
George
|
|
|
|
|
FileStream won't be disposed until GC collects.
|
|
|
|
|
Thanks N a v a n e e t h!
So, you mean the benefit is -- if the wrapper class implements IDispoable, we could enforce "using" block on the wrapper class to Dispose the resource immediately?
regards,
George
|
|
|
|
|
Yes. It's always good to call Dispose() if it is provided.
|
|
|
|
|
Great N a v a n e e t h!
Question answered.
regards,
George
|
|
|
|
|
This issue is with respect to Windows Application
I have 2 comboBox in the same row of a DatagridView. When I attach an event one of them, the other comboBox also fires the same event. How can I attach an event only to a particular combo in a DataGridView
|
|
|
|
|
Make use of the sender argument. Check sender is the required combobox. If yes, continue processing.
|
|
|
|
|
what's the diffrence between these two declarations:
ObjectType objectName;
ObjectType objectName=null;
|
|
|
|
|
The first statement means that you are going to declare an object and the second statement mean you are going to dispose an object. If you want to dispose an object then you assign null to that object.
|
|
|
|
|
arslanjatt wrote: The first statement means that you are going to declare an object and the second statement mean you are going to dispose an object. If you want to dispose an object then you assign null to that object.
This not correct. Both means same. Also you don't need to assign NULL to an object which you want to dispose. It is handled automatically and an object will be garbage collected when nothing is referring it.
|
|
|
|
|
Hi Navaneeth,
Do developers consider it good practise to always initialise an instance to null when they are declared?
For example, if I always declared variables, and set them to null as below:
<br />
Class class1 = null;<br />
would it be bad practice and/or any slower than:
<br />
Class class1;<br />
|
|
|
|
|
Hello Mark,
MarkBrock wrote: Do developers consider it good practise to always initialise an instance to null when they are declared?
I am not sure, but I never used to assign NULL when it is declaring.
object obj;
object obj1 = null;
Console.WriteLine(obj == null);
Console.WriteLine(obj1 == null); This won't compile. Compiler will give error saying obj is not assigned. So easily I can find out uninitialized variables. But if you assign NULL to it and forget to initialize, compiler won't give any error, but you will end with a NullReferenceException at the runtime. I think performance wise there won't be any difference.
|
|
|
|
|
It's not good practice to initialize a variable to null in .NET because it's already done automatically by the CLR. It just wastes an instruction because it's redundant and unnecessary. See Effective C# by Bill Wagner.
|
|
|
|
|
so they're the same in memory but not in cpu usage you mean?
|
|
|
|
|
|
|
Alan!I wanted to mark ur anwer as reply or sth like that,and I clicked
report this message link
is there any way to undo it?
|
|
|
|
|
Ah, so THAT'S why the Code Project agents showed up at my place with a warrent!
|
|
|
|
|
They both are same. If you are using first one, compiler will give error. But for second one, you will get error only in the runtime.
modified on Tuesday, June 17, 2008 3:18 AM
|
|
|
|
|
Hi,
I have to build a windows application in C#. What the application needs to do is print doc files even Microsoft word is not installed. Do you guys have any solution. Please help me out. Thanks in advance.
regard,
Arslan Ilyas
|
|
|
|
|
.doc is a proprietry format, you need MS word to access it. You may be able to do it with the free "word viewer" but I don't know.
In theory it's possible to reverse engenieer the .doc format and write your own parser to read and print it, but that won't be easy. OpenOffice have obviously done something like that as they can handle word files, so you could start by looking at their source code.
There may be 3rd party libraries out that that provide this kind of functionality (search google), but it's not built into .net as standard.
Simon
|
|
|
|
|
Hi
is there any way to add references to my project by code (at runTime)?
thanks.
|
|
|
|
|
|