C beginner's tutorial series 10: access modifiers
Conquer the South Pole 2020-11-26 20:43:01

This article is about C# Simple tutorial for beginners , This is the first 10 piece . Thank you for watching. , Remember to follow me , There are more tutorial articles to follow , thank you .

The environment of this paper is Visual Studio 2019.

One 、 Why use access modifiers

Access modifiers or access specifiers are used to define the visibility of class properties or methods . occasionally , You may not want other programs to see properties or methods of a class . under these circumstances ,C# You can set modifiers on class properties and methods . Class modifiers can restrict access to , So that other programs can't see the properties or methods of the class .

There are several kinds of access modifiers :

public: Unlimited access .

private: Access is limited to the current class .

protected: Access is limited to the current class or types derived from the current class .

internal: Access is limited to the current assembly .

protected internal: Access is limited to the current assembly or types derived from the current class .

private protected: Access is limited to the current class or types derived from the current class in the current assembly .

Two 、 When the access modifier cannot be accessed

When the access modifier cannot be accessed , You can't compile , also IDE It will remind you of the powerful perception . As shown by the arrow below :

C# Beginner's course series 10: Access modifier


C# Beginner's course series 10: Access modifier


This situation , You just need to modify it to the access modifier you need .

3、 ... and 、 When multiple access modifiers come together

Multiple access modifiers are used together :protected internal and private protected, Here's the picture :

C# Beginner's course series 10: Access modifier


C# Beginner's course series 10: Access modifier


?

So what's the special purpose of this access modifier ?

Based on my personal experience , I don't understand the special effect . In my concept and experience , It's just entertainment , It's no use farting , If there are mistakes, please point out in the comments .

So when these multiple access modifiers are combined , The range is getting bigger , Still smaller , There is still a new scope ? The answer is to take it as far as possible .

such as protected internal, You think of it as internal Just to understand ,internal Where it can be used , that protected internal It's where it's used .

Four 、 Access the usage scenarios of modifiers

For a novice , It's all about the whole project public, It's nothing unusual .

If you want to misuse public, Every rookie can do the best , Make people speechless . This is the flood of power .

How to make sure you don't have too much power , Where to use what modifiers , This requires that you have the ability to control your own code .

With your rich experience 、 The improvement of coding level , The depth of object oriented , You'll find the magic of other access modifiers .

In a way , How flexible are you with access modifiers , The profile reflects the level of your programming .

About the specific use of modifiers , I found that I didn't know how to write it in the article for a while .. This makes me faint . Maybe my level is not good enough .

Have a good meal .
版权声明
本文为[Conquer the South Pole]所创,转载请带上原文链接,感谢
https://fheadline.com/2020/11/20201126204121531d.html
相似文章

2021-08-09