Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A using block, with an object initializer block in the constructor

Tags:

c#

If you call a constructor from a within a using statement the object is automatically disposed is wrapped it with a try/catch block. That is an object initializer block in the constructor.

But what becomes with member types that are initialized in the same statement? e.g:

  class TypeThatThrowsAnException 
  {
       public TypeThatThrowsAnException() 
       {
           throw new Exception();
       }
  }

  using (SomeDisposable ds = new SomeDisposable() {
       MemberThatThrowsAnException m = new TypeThatThrowsAnException()
  })
  {
       // inside using statement
       ds.doSomething();
  }

What happens with MemberThatThrowsAnException when it throws an exception when SomeDisposable is initialized, i.e., the code block is executed?

And does it make any difference if we call those members constructors outside the scope of the using block?

  class TypeThatThrowsAnException 
  {
       public TypeThatThrowsAnException() 
       {
           throw new Exception();
       }
  }

  class SomeClass 
  {
       public static TypeThatThrowsAnException StaticMember 
       {
          get
          {
               return new TypeThatThrowsAnException();
          }
       }
  } 

  using (SomeDisposable ds = new SomeDisposable() {
       MemberThatThrowsAnException = SomeClass.StaticMember
  })
  {
       // inside using statement
       ds.doSomething();
  }

In some scenarios this can be pretty nice and readable, but I would like to know if thare are any caveats or pitfalls in this way. Or that it is a no-go all the way. Besides that you need to keep the readability in mind.

like image 655
Caspar Kleijne Avatar asked Jul 04 '11 06:07

Caspar Kleijne


People also ask

What is the use of initializer block in Java?

In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created.

What is initializers in Java?

In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code. Java offers two types of initializers, static and instance initializers.

How do I create an instance block and initializer?

The instance initializer block is created when instance of the class is created. The instance initializer block is invoked after the parent class constructor is invoked (i.e. after super() constructor call). The instance initializer block comes in the order in which they appear.

What is the difference between initializer and constructor?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.


1 Answers

Object initializers are in some sense a red herring here... but they're one example of where a problem is avoidable.

The object isn't "guarded" by the using statement until the resource acquisition expression has completed normally. In other words, your code is like this:

SomeDisposable tmp = new SomeDisposable();
tmp.MemberThatThrowsAnException = new TypeThatThrowsAnException();

using (SomeDisposable ds = tmp)
{
    // Stuff
}

That's more obviously problematic :)

Of course the solution is to assign the property inside the using statement:

using (SomeDisposable ds = new SomeDisposable())
{
    MemberThatThrowsAnException = new TypeThatThrowsAnException();
    // Stuff
}

Now we're only relying on the constructor of SomeDisposable to clean up after itself if it ends up throwing an exception - and that's a more reasonable requirement.

like image 142
Jon Skeet Avatar answered Sep 19 '22 12:09

Jon Skeet