Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is scenario where we can use private class in c#?


What I have tried:

I want to know in which scenario private class can be used??
Posted
Updated 2-May-16 22:28pm

You use a private class when it's contained within an existing class and you want it freely available only to that class. Private classes are "public" within the containing class, but are not accessible outside it.
For example:
C#
public class MyClass
   {
   private class ContainedClass
      {
      ...
      }
   private ContainedClass totalyLegal = new ContainedClass();
   public void MyMethod()
      {
      totalyLegal.Property = value;
      totalyLegal.Method();
      }
   }
public class MyOtherClass
   {
   private MyClass mc = new MyClass();
   private MyClass.ContainedClass generateCompilerError = new MyClass.ContainedClass();
   }
 
Share this answer
 
See, for instance, this page: Private class in C#[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900