Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are C# "using" statements most useful?

So a using statement automatically calls the dispose method on the object that is being "used", when the using block is exited, right?

But when is this necessary/beneficial?

For example let's say you have this method:

public void DoSomething()
{
    using (Font font1 = new Font("Arial", 10.0f))
    {
        // Draw some text here
    }
}

Is it necessary to have the using statement here, since the object is created in the method? When the method exits, wont the Font object be disposed of anyway?

Or does the Dispose method get run at another time after the method exits?

For example if the method was like this:

public void DoSomething()
{
    Font font1 = new Font("Arial", 10.0f);

    // Draw some text here
}

// Is everything disposed or cleared after the method has finished running?
like image 521
John B Avatar asked Apr 15 '09 16:04

John B


People also ask

Are C-sections painful?

You won't feel any pain during the C-section, although you may feel sensations like pulling and pressure. Most women are awake and simply numbed from the waist down using regional anesthesia (an epidural and/or a spinal block) during a C-section. That way, they are awake to see and hear their baby being born.

When do you do C-section?

A caesarean may be recommended as a planned (elective) procedure or done in an emergency if it's thought a vaginal birth is too risky. Planned caesareans are usually done from the 39th week of pregnancy.

How big does a baby have to be for cesarean?

ACOG says ultrasound is no better than a provider's exam in estimating fetal weight, suspected macrosomia should not be an indication for induction of labor, and planned C-sections shouldn't be performed unless the estimated fetal weight is 10 pounds or more in diabetic women or 11 pounds or more in other women.

Does your cervix open during C-section?

During elective (planned) caesarean sections, some obstetricians routinely dilate the cervix intraoperatively, using sponge forceps, a finger, or other instruments, because the cervix of women not in labour may not be dilated, and this may cause obstruction of blood or lochia drainage.


1 Answers

The 'using' statement is most useful when working with unmanaged objects, like database connections.

In this way, the connection is closed and disposed no matter what happens in the code block.

For more discussion, see this article on CodeProject: http://www.codeproject.com/KB/cs/tinguusingstatement.aspx

like image 183
Jeff Fritz Avatar answered Sep 29 '22 16:09

Jeff Fritz