Variable declaration outside the class definition using "using" statement

Viewed 119

I have found some source code on the net and I am not able to understand this usage of using statement outside the class definition:

namespace Artfunkel
{
    using DataErrorsChangedEventManager = WeakEventManager<INotifyDataErrorInfo, DataErrorsChangedEventArgs>;

    public class DataErrorsControl : Control
    {
        private readonly Dictionary<string, CollectionContainer> _errorLookup;
      ...
    }
}

Is it possible to declare variables outside the class definition? There is no var keyword.

This source code is from https://gist.github.com/Artfunkel/868e6a88e37bd9769cd8beb04fd9837f

3 Answers

They're basically creating an alias or alternative name for that closed generic type. Its not a variable declaration, but rather an alternative way to refer to that closed generic type, likely to prevent it from having to be typed all over the place and to make the intent more clear.

Just to add to some of the answers here.

using is a keyword that does multiple things in C#.

Right here, it is being used to alias one type to another name.

In its other application, it is used to force the garbage collector to get rid of the used variable at the end of the scoped block.

using (var conn = new SqlConnection("SomeConnstring"))
{
  // Things happen with conn
}

// conn is guaranteed cleaned up by GC at this point.
Related