Some of the access modifiers in C #

Source: Internet
Author: User
Tags modifiers

Public: Access is not restricted.

1 classpointtest2 {3      Public intx;4      Public inty;5 }6 7 classMAINCLASS48 {9     Static voidMain ()Ten     { OnePointtest p =Newpointtest (); A         //Direct Access to Public members: -p.x =Ten; -P.Y = the; theConsole.WriteLine ("x = {0}, y = {1}", p.x, P.Y);  -     } - } - //output:x = ten, y =
View Code

Protected: Access is limited to types that contain or derive from the containing class.

1 //A protected member of a base class is accessible in a derived class only if access is occurring through a derived class type. 2 classA3 {4     protected intx =123;5 }6 7 classb:a8 {9     Static voidMain ()Ten     { OneA A =NewA (); Ab b =NewB (); -  -         //Error CS1540, because X can only is accessed by the         //classes derived from A. -         //a.x = 10;  -  -         //OK, because this class derives from A. +b.x =Ten; -     } +}
View Code

Internal: Access only Limit to the current assembly.

1 //The sample consists of two files: Assembly1.cs and Assembly1_a.cs. The first file contains an internal base class BaseClass. In the second file, an attempt to instantiate BaseClass will produce an error. 2 //Assembly1.cs3 //Compile with:/target:library4 Internal classBaseClass5 {6     Public Static intIntm =0;7 }8-------------------------------------------------------------------9 //Assembly1_a.csTen //Compile with:/reference:assembly1.dll One classtestaccess A { -    Static voidMain () -    { theBaseClass myBase =NewBaseClass ();//CS0122 -    } -}
View Code
1Use with example1The same file as the file used in, and change the accessibility level of the BaseClass to Public。 Also change the accessibility level of member Intm toInternal. In this example, you can instantiate a class, but you cannot access internal members. 2 //Assembly2.cs3 //Compile with:/target:library4  Public classBaseClass5 {6    Internal Static intIntm =0;7 }8------------------------------------------------9 //Assembly2_a.csTen //Compile with:/reference:assembly1.dll One  Public classtestaccess A { -    Static voidMain () -    { theBaseClass myBase =NewBaseClass ();//Ok. -Baseclass.intm =444;//CS0117 -    } -}
View Code

protected Internal:access is limited to the current assembly or types derived F Rom the containing class. " >protected internal protected Internal:access is limited to the current assembly or types derived from the Cont Aining class. " >: protected Internal:access is limited to the current assembly or types derived from the containing Class. " > Access is restricted to the current assembly or from a category that contains derived types. (the type or member that has the access modifier   protected internal , either from the current assembly or from a type derived from the containing class.) )

Private: Access is limited to the containing type and is a member access modifier. private access is the lowest level of access allowed. Private members are accessible only in the class and struct in which they are declared.

 1 //the Employee class consists of two private data members name and salary. As private members, they can only be accessed through member methods. Add a public method named GetName and Salary so that the private member can be controlled///the access. The name member is accessed through a public method, and the salary member is accessed through a public read-only property.  2 classEmployee23 { 4     Private stringName ="FirstName, LastName"; 5     Private DoubleSalary =100.0; 6  7      Public stringGetName ()8     { 9         returnname;Ten     } One  A      Public DoubleSalary -     { -         Get{returnsalary;} the     } - } -  - classprivatetest + { -     Static voidMain () +     { AEmployee2 e =NewEmployee2 (); at  -         //the data members is inaccessible (private), so -         //they can ' t be accessed as this: -         //string n = e.name; -         //double s = e.salary; -  in         //' name ' is indirectly accessed via method: -         stringn =e.getname (); to  +         //' salary ' is indirectly accessed via property -         Doubles =e.salary; the     } *}
View Code

Some of the access modifiers in C #

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.