-
yah, sorry, hellswraith, my bad, I'm sometimes not very good at explaining to others what I'm talking about! :(
and yes, you guys are right, it's recommended that if using C# you should use temp variables to mimic the with statement in VB when accessing the same thing over and over, then the IL will be practically identical.
-
hmmm ... i am not getting it? for example i have a listbox in C#...in vb.net i would do:
With ListBox1
.items.add("...")
end with
in C# how do i do?
object m = listBox1;
((ListBox)m).items.add("...");
is it it?
-
in your code example, the with statement is only going to save you some typing, which is nice, but it's not going to help your code run faster.
-
One could also use the using method as follows:
PHP Code:
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
-
whats the advantage of that?
does it automaticly dispose the streamreader?
-
-
yah, that's unfortunately, one of those cool things that C# has that VB.NET does not...wish it did.
-
yea im remembering i've read that on a book in the chapter of gdi+...u should always use that on brushes and that things unless u on a paint event...but never though it could be done this way with IO
-
Can be done with everything that implements IDisposable, even your own classes.
-
hmmmm nice...it will for example in loops speed up the process? i mean clean up faster resources?
-
It isn't faster, it's just shorter to write.
From the C# docs:
This code:
Code:
using(Resource r = new Resource()) {
r.DoSomething();
}
is equivalent to this:
Code:
Resource r = new Resource();
try {
r.DoSomething();
} finally {
((IDisposable)r).Dispose();
}
You should avoid object creation and disposal as much as possible in loops anyway.