Tom
Tom

Reputation: 23

Variable declaration outside the class definition using "using" statement

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

Upvotes: 2

Views: 561

Answers (3)

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

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.

Upvotes: 0

Magnetron
Magnetron

Reputation: 8553

It's using using as a alias directive, to create an alias for a namespace or a type.

Read more here, it's the tird use of using: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive

Upvotes: 0

nathan gonzalez
nathan gonzalez

Reputation: 11987

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.

Upvotes: 1

Related Questions