Re: Why does using keyword require a new scope?

Discussion in 'ASP .Net' started by Jip from Paris, Aug 25, 2003.

  1. It is my opinion that the syntax you are proposing, ie :

    > {
    > using AnyDisposableClass adc = new AnyDisposableClass();
    > }


    is less flexible and much more error prone than the one implemented by the
    C# language. Let me explain. The basic syntax for the using statement is :

    using (AnyDisposableExpression)
    {
    }

    This mean you can write (sample 1) :

    AnyDisposableClass myUsedObject = null;

    using (myUsedObject)
    {
    // Do something
    myUsedObject = new AnyDisposableClass();
    // Do other things.
    }

    The compiler will be perfectly happy with this. It already handles the case
    where myUsedObject is a null reference on scope exit. Now consider this
    other exemple (sample 2) :

    AnyDisposableClass myUsedObject = null;

    using (myUsedObject)
    {
    // Do something that might or not initialize the myUsedObject
    if (...)
    {
    myUsedObject = new AnyDisposableClass();
    }
    // Do other things.
    }

    Here, the myUsedObject is initialized in an inner scope of the using scope.
    So, what is the syntax you are expecting here ? I assume this would be
    something like (sample 3)

    AnyDisposableClass myUsedObject = null;

    // Open the implied using scope
    {
    // Some code ignored here

    using (myUsedObject)

    // Do something that might or not initialize the myUsedObject
    if (...)
    {
    myUsedObject = new AnyDisposableClass();
    }
    // Do other things.
    }
    // Closed the implied using scope

    And NOT this one which means something else (sample 4) :

    AnyDisposableClass myUsedObject = null;

    {
    // Do something that might or not initialize the myUsedObject
    if (...)
    {
    // Placing the using statement here would dispose on if scope exit.
    using (myUsedObject)

    myUsedObject = new AnyDisposableClass();
    }
    // Do other things.
    }

    I believe that your syntax is much more error prone than the existing one.
    Let's go back to sample 3 and now assume that we had to modify the code for
    some rreason not related to the using statement. This could lead us to ad
    another scope around the using statement without clear knowledge of what we
    are doing (sample 5)

    AnyDisposableClass myUsedObject = null;

    // NOT any more the implied using scope
    {
    // New condition handling added that delimits a new implied using scope.
    if (...)
    {
    // Some code ignored here

    using (myUsedObject)

    // Do something that might or not initialize the myUsedObject
    if (...)
    {
    myUsedObject = new AnyDisposableClass();
    }
    }
    else
    {
    // Some code ignored here
    }
    // Do other things.
    }
    // Closed the NOT any more implied using scope

    Once again its my opinion that making the using statement scope explicit is
    a good thing as far as robustness is concerned.

    "Dave Goldstein" <> wrote in message
    news:...
    > Why this nuisance:
    >
    > using(AnyDisposableClass adc = new AnyDisposableClass()) {
    > }
    >
    > when it could have been created this way:
    >
    > using AnyDisposableClass adc = new AnyDisposableClass();
    >
    > and then gets disposed when exiting scope?
    >
    > I believe C# allows anonymous scopes, so the same task could therefore
    > have been accomplished this way:
    > {
    > using AnyDisposableClass adc = new AnyDisposableClass();
    > }
    >
    > Any thoughts/responses? I find the using clause provided to be really
    > aesthetically displeasing and clumsy, as the layer of nesting is
    > completely unnecessary ecept for the implementation detail of when the
    > variable is actually initialized (i.e. scope could exit before the
    > variable is assigned; so what! null means do nothing!)
    >
    > To simplify things, a readonly/const could be implied or required by
    > such a use case.
    >
    > -d
    >
    Jip from Paris, Aug 25, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herman Eldering

    Re: Why does using keyword require a new scope?

    Herman Eldering, Aug 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    965
    Herman Eldering
    Aug 24, 2003
  2. Paul Opal
    Replies:
    12
    Views:
    940
    Paul Opal
    Oct 11, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    6
    Views:
    449
    Peter Otten
    May 10, 2007
  5. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    349
Loading...

Share This Page