LoginSignup
3
2

More than 5 years have passed since last update.

naming > クラス名をnamespace名と同じにはしないこと

Last updated at Posted at 2015-08-30

Unityで処理を別ファイルにしようとしていて、namespaceとクラス名をどういうふうにつけようか考えているなか、同じ名前にしようかと思った。

調べているうちに以下を見つけた。

Do not name a class the same as its namespace, Part One

namespace MyContainers.List
{
    public class List {  }
}

上記がまずい理由として以下の理由があげられている

Part One: Collisions amongst referenced assemblies:

以下の実装において、Fooを参照しようとして、どのFoo?となってしまうとのこと。

// Foo.DLL:
namespace Foo { public class Foo { } }

// Bar.DLL:
namespace Bar { public class Foo { } }

// Blah.DLL:
namespace Blah 
{
  using Foo;
  using Bar;
  class C { Foo foo; }
}

回避策は上記リンクであるようだが、結局は「最初からFoo.DLLの著者がguidelinesに基づき、かつ、typeとnamespaceを同じようにしないことで避けられる」とのこと。

The problem can be avoided in the first place by the authors of Foo.DLL following the guidelines and not naming their type and their namespace the same thing.

using Fooとusing Barを同時にするのをやめてFoo::Foo, Bar::Fooという書き方にするのはどうなのだろうか?

このあたりはどういうのが標準的かまだ良くわかっていない。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2