Concept of Private class in C#

C#.NetAccess Modifiers

C# Problem Overview


Can private classes exist in C#, other than in Inner classes?

C# Solutions


Solution 1 - C#

Simply NO. Nothing unless its in a nested Class

  • Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition.

  • Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself — it always has access to itself and all of its own members.

  • Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.

  • Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.


    You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute.

Solution 2 - C#

No, there isn't. You cannot have a private class unless it is nested.

Solution 3 - C#

In what scenario other then for an innter class would you like to have a 'private' class ?

You can use the internal modifier to create a class that is only visible in the current assembly.

// the class below is only visible inside the assembly in where it was declared
internal class MyClass
{
}

Solution 4 - C#

No. What would the scope of such a class be?

Solution 5 - C#

We can declare a class as Private inside other class. kindly find the code below on how to achieve the same:

 public class Class1
  {
    temp _temp ;
    public Class1()
    {
      _temp = new temp();   
    }    

    public void SetTempClass(string p_str, int p_Int)
    {
      _temp.setVar(p_str, p_Int);
    }

    public string GetTempClassStr()
    {
      return _temp.GetStr();
    }

    public int GetTempClassInt()
    {
      return _temp.GetInt();
    }

    private class temp
    {
      string str;
      int i;

      public void setVar(string p_str, int p_int)
      {
        str = p_str;
        i = p_int;
      }

      public string GetStr()
      {
        return str;
      }

      public int GetInt()
      {
        return i;
      }
    }
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinod BhattView Question on Stackoverflow
Solution 1 - C#Shekhar_ProView Answer on Stackoverflow
Solution 2 - C#Darin DimitrovView Answer on Stackoverflow
Solution 3 - C#Frederik GheyselsView Answer on Stackoverflow
Solution 4 - C#PatmanView Answer on Stackoverflow
Solution 5 - C#SPARTANView Answer on Stackoverflow