Learn Access Modifiers in C#

Access modifiers in C# are keywords used to define the accessibility or visibility of classes, methods, variables, and other members. They help control how and where these members can be accessed. Here’s a concise overview of the main access modifiers in C#:

1. Public

  • Description: Accessible from anywhere in the program.
  • Use Case: When you want a member to be universally accessible.
  • Example

2. Private

  • Description: Accessible only within the containing class.
  • Use Case: To encapsulate data and prevent external access.
  • Example

3. Protected

  • Description: Accessible within the containing class and by derived classes.
  • Use Case: When you want to allow access to subclasses but not to other classes.
  • Example

4. Internal

  • Description: Accessible only within the same assembly.
  • Use Case: To restrict access to the same project or library.
  • Example

5. Protected Internal

  • Description: Accessible within the same assembly or by derived classes in other assemblies.
  • Use Case: A combination of protected and internal access.
  • Example

6. Private Protected

  • Description: Accessible within the containing class or derived classes in the same assembly.
  • Use Case: To limit access to derived classes within the same assembly.
  • Example

Summary Table

ModifierSame ClassDerived ClassSame AssemblyOther Assemblies
public
private
protected
internal
protected internal✔ (if derived)
private protected

These modifiers are essential for implementing encapsulation and controlling access to your code effectively.

ASP.NET 10.0 Hosting Recommendation

ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts