Is this dumb?

  • Thread starter Thread starter Mike Labosh
  • Start date Start date
M

Mike Labosh

Dim message As String = File.OpenText("FileNotification.html").ReadToEnd()

Because the StreamReader never got closed?
 
Hi Mike Labosh,
Dim message As String =
File.OpenText("FileNotification.html").ReadToEnd()

Because the StreamReader never got closed?

Yes. Better use "using"
(and if the file does not exist, you also have some trouble...)

string s;
using(StreamReader sr = File.OpenText("fn.txt))
{
s = sr.ReadToEnd();
}


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Dim message As String =
Yes. Better use "using"

(and if the file does not exist, you also have some trouble...)

The file is a static html template that's part of my assembly so it's
guaranteed to be there.
string s;
using(StreamReader sr = File.OpenText("fn.txt))
{
s = sr.ReadToEnd();
}

I'm pretty familiar with C#, but what's this using() {} thing do?
 
Hi Mike Labosh,
Ok, am I nuts here? This seems silly to me. It requires 3 lines of
code:

using(//stuff)
{
//my stuff
}

To just do this?!?

Stuff.Dispose();

No, it requires 9 lines!!!


<code>
StreamReader sr; = File.OpenText("fn.txt)
try
{
sr = File.OpenText("fn.txt);
// do some stuff...
}
finally{
IDspose idsp = (IDispose) sr;
sr.Dispose();
}
</code>


So therefor 3 lines is better.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Well write the fully equivelent code - yours isn't exception proof

Thing Stuff = new Thing();
try
{
// my stuff
}
finally
{
Stuff.Dispose();
}

See? Its a short cut

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:

Ok, am I nuts here? This seems silly to me. It requires 3 lines of code:

using(//stuff)
{
//my stuff
}

To just do this?!?

Stuff.Dispose();
 
Mike Labosh said:
Ok, am I nuts here? This seems silly to me. It requires 3 lines of code:

using(//stuff)
{
//my stuff
}

To just do this?!?

Stuff.Dispose();

No, this

using (Stuff stuff = new Stuff())
{
//my stuff
}

is equivalent to

Stuff stuff = new Stuff();
try
{
//my stuff
}
finally
{
stuff.Dispose();
}

David
 
No, this
using (Stuff stuff = new Stuff())
{
//my stuff
}

is equivalent to

Stuff stuff = new Stuff();
try
{
//my stuff
}
finally
{
stuff.Dispose();
}

Duh, that's right. 's what I get for having my head up a phone number
parser.
 
Hi David Browne,
is equivalent to

Stuff stuff = new Stuff();
try
{
//my stuff
}
finally
{
stuff.Dispose();
}

Nope...
Mostly Dispose is a hidden member, and therfor cannot be called directly!


Stuff stuff = new Stuff();
try
{
//my stuff
}
finally
{
IDisposable disp = (IDisposable) stuff;
disp.Dispose();
}


To verify this, look into idlasm or reflector.


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Actually, if we're going to be picky ;-)

for value types, if the type's Dispose method is public then it doesn't cast to the interface as that would require a box operation.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>


Stuff stuff = new Stuff();
try
{
//my stuff
}
finally
{
IDisposable disp = (IDisposable) stuff;
disp.Dispose();
}


To verify this, look into idlasm or reflector.
 
Jochen Kalmbach said:
Hi David Browne,


Nope...
Mostly Dispose is a hidden member, and therfor cannot be called directly!

Sometimes yes, sometimes no. But if you want to be picky, it's really more
like

{
MyDisposable d = new MyDisposable();
try
{
//whatever
}
finally
{
#if MyDisposable_IS_A_VALUETYPE && MyDISPOSABLE_DISPOSE_IS_PUBLIC
d.Dispose();
#else
((IDisposable)d).Dispose();
#endif
}
}

Since the variable is scoped to the using block and the compiler does not
invoke Dispose through the IDisposable interface for structs with public
Dispose (since it saves boxing operation).

David
 
Hum, it was my understanding that when Dispose() is not public, it's because
it should not be called directly and that some other method, usually
Close(); should be called instead of making an «
((IDisposable)d).Dispose(); ».

S. L.
 
<"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam
please)> said:
Hum, it was my understanding that when Dispose() is not public, it's because
it should not be called directly and that some other method, usually
Close(); should be called instead of making an «
((IDisposable)d).Dispose(); ».

Not IMO. Using Dispose allows a uniform method of clearing up simply,
especially in C# with the "using" statement. It's rarely clear exactly
why Dispose is declared with explicit interface implementation in any
particular class, but I don't think that means you shouldn't use it.
 
Back
Top