Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept of Private class in C#

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

like image 640
Vinod Bhatt Avatar asked Feb 05 '11 11:02

Vinod Bhatt


People also ask

What is meant by private class?

The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

What is private in C?

private (C++) When preceding a list of class members, the private keyword specifies that those members are accessible only from member functions and friends of the class. This applies to all members declared up to the next access specifier or the end of the class.

What is a private class in programming?

A private means that only that class has direct access to the variable, everything else needs a method/function to access or change that data.

What is the use of private class?

Private classes are useful for creating building blocks that are implementing internal functionality that you don't necessarily want visible to other projects using a library.


1 Answers

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.
like image 176
Shekhar_Pro Avatar answered Oct 04 '22 12:10

Shekhar_Pro