A singleton in C # is "unavailable" if not in the same namespace?

I tried to implement a singleton class as follows (I am using VS2008 SP1):

namespace firstNamespace
{
   class SingletonClass
   {
      private SingletonClass() {}

      public static readonly SingletonClass Instance = new SingletonClass();
   }
}

When I want to access it from a class in another namespace (it seems that this is a problem, it works in the same namespace), for example:

namespace secondNamespace
{
   ...
   firstNamespace.SingletonClass inst = firstNamespace.SingletonClass.Instance;
   ...
}

I get a compiler error:

error CS0122: 'firstNamespace.SingletonClass' is inaccessible due to its protection level

Does anyone have any ideas how to solve this?

Thank you very much in advance!

+5
source share
6 answers

You are missing a keyword publicfrom your class definition.

+10
source

. , , .

+3

SingletonClass , , , .

class SingletonClass

public class SingletonClass
+2

class SingletonClass

public class SingletonClass

,

:

public sealed class SingletonClass

:

+2

SingletonClass . /.

. , (= Assembly =.dll) . .

. , , mscorlib.dll System.dll System.

, Visual Studio, .

. Visual Studio .

+1

SingletonClass class , .

: , msdn:

, , , . , , . , , . , public , . , . - .

-1

All Articles