usingを使用する時にnamespaceの内側と外側に書いた際の違いが分からなかったので調べてみました。
結論
検索の順序が変わる
内側から順に検索されて該当箇所が発見された時点で検索終了
検証
参考ページにある処理を試してみました。
まずはnamespace Testの中にMathクラスを作成
次にusing Systemをnamespaceの内側、外側にそれぞれ記載。
Mathクラスを呼び出して動作を確認してみます。
結果はnamespaceの外側にusing Systemを記載すると、自作したMathクラスが先に検索されて呼び出されました。
検索順序
こちらも参考ページより抜粋した処理となります。
検索順序は以下のようになるようです。
Test.cs
using System;
using System.Collections.Generic;
using System.Linq;
//using MyCorp.TheProduct; <-- uncommenting this would change nothing
using MyCorp.TheProduct.OtherModule;
using MyCorp.TheProduct.OtherModule.Integration;
using ThirdParty;
namespace MyCorp.TheProduct.SomeModule.Utilities
{
class C
{
Ambiguous a;
}
}
- クラスCの内部
- 名前空間の型 MyCorp.TheProduct.SomeModule.Utilities
- 名前空間の型 MyCorp.TheProduct.SomeModule
- 名前空間の型 MyCorp.TheProduct
- 名前空間の型 MyCorp
- グローバル名前空間
- using系