Access modifiers for C # encapsulation

Source: Internet
Author: User
Tags modifiers

encapsulation is defined as "enclosing one or more items in a physical or logical package". In the object-oriented programming methodology, encapsulation is designed to prevent access to the implementation details.

Abstraction and encapsulation are the related features of object-oriented programming. Abstraction allows for the visualization of related information, and encapsulation enables developers to achieve the desired level of abstraction .

C # Encapsulation Sets the user's access rights to the specific needs, and is implemented by access modifiers .

An access modifier defines the scope and visibility of a class member. The access modifiers supported by C # are as follows:

    • Public: All objects can be accessed;
    • Private: The object itself can be accessed within the object;
    • Protected: Only the Class object and its child class objects can be accessed
    • Internal: An object of the same assembly can be accessed;
    • Protected internal: An object within an assembly, or the class object and its subclasses can access

If no access modifier is specified, the default access modifier for the class member is used, which is private.

Public access modifier

The public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by an external class.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram1 { Public intA;  Public intb;  Public intAddData () {returnA +b; }         Public voidDisplay () {Console.WriteLine ("a: {0}", a); Console.WriteLine ("B: {0}", B); Console.WriteLine ("and: {0}", AddData ()); }    }    classProgram2 {Static voidMain (string[] args) {PROGRAM1 a=NewProgram1 (); A.A=3; A.B=5;            A.display ();        Console.ReadLine (); }    }}

When the above code is compiled and executed, it produces the following results:

A:3b:5 and: 8

In the above program, the variables and functions in class program1 are declared public , so the main () function in the following program2 can be accessed using instance a of the Program1 class.

Private access modifier

Only a function in the same class can access its private members, even instances of the class cannot access its private members. If you change the declaration of the above program class PROGRAM1 to private, the following prompt appears when you access them using instances of the class:

Under this declaration, in order to use (manipulate) these values in other classes, you can write functions that manipulate these values under the class where the values are located, and then access the functions in other classes, as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram1 {Private intA; Private intb;  Public voidAcceptdetails () {Console.WriteLine ("Please enter a:"); A=convert.toint16 (Console.ReadLine ()); Console.WriteLine ("Please enter B:"); b=convert.toint16 (Console.ReadLine ()); }         Public intAddData () {returnA +b; }         Public voidDisplay () {Console.WriteLine ("a: {0}", a); Console.WriteLine ("B: {0}", B); Console.WriteLine ("and: {0}", AddData ()); }    }    classProgram2 {Static voidMain (string[] args) {PROGRAM1 a=NewProgram1 ();            A.acceptdetails ();            A.display ();        Console.ReadLine (); }    }}

The results of the operation are as follows:

Please enter A:5 Please enter b:4a:5b:4 and: 9

  

Protected access Modifiers

The Protected access modifier allows subclasses to access the member variables and member functions of its base class. This helps to implement inheritance.

Internal access Modifiers

The Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current program. In other words, any member with the internal access modifier can be defined to be accessed by any class or method within the application defined by that member.

Protected Internal access modifier

The Protected Internal access modifier allows access in this class, derived classes, or assemblies that contain the class. This is also used to implement inheritance.

  

Access modifiers for C # encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.