Guillaume Slashy
Guillaume Slashy

Reputation: 3624

Load imports outside/inside of namespace if there is only one?

I got ONLY 1 Namespace and these 2 different codes :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace blabla
{
    [...]
}

and

namespace blabla
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.IO;

    [...]
}

I don't see any difference at all here but it that really the case ? I mean about performance or whatever

Upvotes: 2

Views: 79

Answers (2)

marc wellman
marc wellman

Reputation: 5886

It's a matter of scope declaration.

In the first case you are "declaring" the using statements on a file-level which means that they are valid for several namespaces you are able to declare in this file.

In the second case your using statements are only valid inside the defined (namespace-)scope.

Hope this helps :)

Upvotes: 1

Tamir
Tamir

Reputation: 2533

there is no difference in terms of performance. Namespace is the way of code organization and scoping. So in your case, using states will be defined globally in first snippet and inside namespace for the second. In both cases, physical assemblies will be referenced equally in both cases.

Upvotes: 2

Related Questions