2

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

1

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

1 Comment

The garbage collector is not involved in IDisposable use of using. It is definitely not guaranteed to be cleaned up by GC at that point. It might be, but there is no such guarantee. This use of using only adds a call to IDisposable.Dispose() for the object at the end of the scope, it does not involve GC directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.