Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does using(object obj = new Object()) mean?

What does this statement means in C#?

        using (object obj = new object())
        {
            //random stuff
        }
like image 687
Ricardo Avatar asked Jan 12 '10 18:01

Ricardo


People also ask

What is the meaning of new object?

New-Object creates the object and sets each property value and invokes each method in the order that they appear in the hash table. If the new object is derived from the PSObject class, and you specify a property that does not exist on the object, New-Object adds the specified property to the object as a NoteProperty.

What does OBJ mean in C#?

Object, in C#, is an instance of a class that is created dynamically. Object is also a keyword that is an alias for the predefined type System. Object in the . NET framework. The unified type system of C# allows objects to be defined.

What does Object [] mean Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.

How do you add two objects in Java?

However, in Java there is no such thing as operator overloading. It would have to be written similar to i3 = i1. add(i2) (where add is a method that does the appropriate logic and creates the new object).


1 Answers

It means that obj implements IDisposible and will be properly disposed of after the using block. It's functionally the same as:

{
  //Assumes SomeObject implements IDisposable
  SomeObject obj = new SomeObject();
  try
  {
    // Do more stuff here.       
  }
  finally
  { 
    if (obj != null)
    {
      ((IDisposable)obj).Dispose();
    }
  }
}
like image 132
Justin Niessner Avatar answered Sep 19 '22 07:09

Justin Niessner