Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the C# access modifiers

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to share with you the relevant knowledge points of the C# access modifier, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

Access modifier 1, definition

First, let's take a look at the official definition of access modifiers:

All types and type members have an accessible level. This level controls whether you can use them from your assembly or other code in other assemblies.

To put it bluntly, access modifiers are equivalent to permissions.

For example, in a company, your superiors can do what you can do, but you may not be able to do what your superiors can do. You can touch the fish, the superior can also touch the fish; the superior can pay the salary, but you can only get the salary.

The same is true in programming, where access modifiers determine which code you can use at will and which are not allowed to use.

2. Permissions

There are five access modifiers in C #, namely public, private, protected, internal, private protect, and protected internal.

Public is equivalent to public authority and can be used anywhere; private is equivalent to private authority, and it is better to know some secrets yourself.

Second, encapsulation

As we all know, object-oriented programming languages have three major features, namely, encapsulation, inheritance and polymorphism.

(1) definition

Enclose one or more projects in a room or in a logical package.

In object-oriented programming methodology, encapsulation is designed to prevent access to implementation details.

Encapsulation means that we put the written and associated program code into a class, hiding the properties and implementation details in the object. When you want to use one of these functions, you just need to call this class.

(2) Encapsulation and access modifiers

So what is the relationship between encapsulation and access modifiers?

The encapsulation mentioned above is to put the relevant complete code together, as long as the invocation of the associated class is inseparable from the discussion of access modifiers.

For example, if we encapsulate a class where there are variables or methods that everyone can use, we can use public to modify variables and methods; but there are some things that I hope can only be used in this class, and no one else has the right to use them, then we can use private to modify variables and methods.

Therefore, you need to specify access modifiers based on the object you want to use, which is also an important point in encapsulation.

(3) advantages

Improve data security-the caller cannot modify a private member property, ensuring the data security of the private attribute.

Improve code reusability-encapsulate a series of related code in which the caller wants to implement the function as long as he knows the calling method to use. It is easy to be reused and reduces the generation of a lot of code.

Hide the specific implementation-the caller only needs to know the calling method, not until the specific implementation process.

These are all the contents of the article "what are the C# access modifiers?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report