the key word "using" questiton?
-
Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }
-
Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }
-
Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }
// look at this: using( Brush foreBrush = new SolidBrush(this.ForeColor) ) // here foreBrush is disposed eh?? how come it can still be used? using( Brush backBrush = new SolidBrush(this.BackColor) )
-
Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }
The using statement is translated by the compiler from
using(Brush foreBrush = new SolidBrush(this.ForeColor))
{
...
}to
Brush foreBrush = null;
try
{
foreBrush = new SolidBrush(this.ForeColor);
...
}
finally
{
if (foreBrush != null)
((IDisposable)foreBrush).Dispose();
}foreBrush gets disposed in the finally block. Because you are using it within the block, it works. Your code will get translated into something like
Brush foreBrush = null;
try
{
foreBrush = new SolidBrush(this.ForeColor);
Brush backBrush = null;
try
{
backBrush = new SolidBrush(this.BackColor);
...
}
finally
{
if (backBrush != null)
((IDisposable)backBrush).Dispose();
}
}
finally
{
if (foreBrush != null)
((IDisposable)foreBrush).Dispose();
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
The using statement is translated by the compiler from
using(Brush foreBrush = new SolidBrush(this.ForeColor))
{
...
}to
Brush foreBrush = null;
try
{
foreBrush = new SolidBrush(this.ForeColor);
...
}
finally
{
if (foreBrush != null)
((IDisposable)foreBrush).Dispose();
}foreBrush gets disposed in the finally block. Because you are using it within the block, it works. Your code will get translated into something like
Brush foreBrush = null;
try
{
foreBrush = new SolidBrush(this.ForeColor);
Brush backBrush = null;
try
{
backBrush = new SolidBrush(this.BackColor);
...
}
finally
{
if (backBrush != null)
((IDisposable)backBrush).Dispose();
}
}
finally
{
if (foreBrush != null)
((IDisposable)foreBrush).Dispose();
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
S. Senthil Kumar wrote: Brush foreBrush = null; try { foreBrush = new SolidBrush(this.ForeColor); ... } finally { if (foreBrush != null) ((IDisposable)foreBrush).Dispose(); } which begs a question, what if
foreBush
does not implementIDisposable
? It will simply crash ( throw exception) ? David -
The using statement is translated by the compiler from
using(Brush foreBrush = new SolidBrush(this.ForeColor))
{
...
}to
Brush foreBrush = null;
try
{
foreBrush = new SolidBrush(this.ForeColor);
...
}
finally
{
if (foreBrush != null)
((IDisposable)foreBrush).Dispose();
}foreBrush gets disposed in the finally block. Because you are using it within the block, it works. Your code will get translated into something like
Brush foreBrush = null;
try
{
foreBrush = new SolidBrush(this.ForeColor);
Brush backBrush = null;
try
{
backBrush = new SolidBrush(this.BackColor);
...
}
finally
{
if (backBrush != null)
((IDisposable)backBrush).Dispose();
}
}
finally
{
if (foreBrush != null)
((IDisposable)foreBrush).Dispose();
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Hi , Senthil , does that mean, after foreBrush is disposed, if following code gona use it , then it will be constucted again ??
-
S. Senthil Kumar wrote: Brush foreBrush = null; try { foreBrush = new SolidBrush(this.ForeColor); ... } finally { if (foreBrush != null) ((IDisposable)foreBrush).Dispose(); } which begs a question, what if
foreBush
does not implementIDisposable
? It will simply crash ( throw exception) ? David -
Hence why only those objects which implement IDisposible can be used within a using block. In this case foreBrush is an instance of SolidBrush and that implements IDisposible
I know. I just thought that it could be like
finally { IDisposable thing; if ((thing = foreBrush as IDisposable) != null) thing.Dispose(); }
OK, it's better how it is. David
-
I know. I just thought that it could be like
finally { IDisposable thing; if ((thing = foreBrush as IDisposable) != null) thing.Dispose(); }
OK, it's better how it is. David
The code wouldn't compile if Brush didn't implement IDisposable. The compiler would complain that the type used in the "using" statement cannot be disposed, or something similar to that. So there's no question of a runtime crash. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Hi , Senthil , does that mean, after foreBrush is disposed, if following code gona use it , then it will be constucted again ??
No, it means that foreBrush will be disposed only at the end of its block. So
using (Brush foreBrush = new Brush())
using (Brush backBrush = new Brush())
{
}// dispose backBrush
// dispose foreBrush as the block ends here.Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
The code wouldn't compile if Brush didn't implement IDisposable. The compiler would complain that the type used in the "using" statement cannot be disposed, or something similar to that. So there's no question of a runtime crash. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
S. Senthil Kumar wrote: The code wouldn't compile That's exactly the answer I was looking for. Thanks. David
-
Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }
there is no ";" after the
using( Brush foreBrush = new SolidBrush(this.ForeColor) )
. it behaves like the if clause. you can use it like this:if (x == true) if (y==true) { ... code in block ... }
or like this:if (x == true) { if (y==true) { ... code in block ... } }
if there would be a semicolon right after the using statement your assumption that the foreBrush should be disposed would be true. /cadi