Private Protect Partial Internal Public 区别

时间:2022-07-12 20:31:28

摘自:MSDN

 

The situation is similar to personal secrets (shared only with friends), family secrets (shared with friends and children), and nonsecrets (shared with anybody), respectively.


A Partial:It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.


please see the following example code:

            public partial class CoOrds
           {
                private int x;
                private int y;

               public CoOrds(int x, int y)
              {
                   this.x = x;
                   this.y = y;
              }
           }

           public partial class CoOrds
         {
              public void PrintCoOrds()
             {
                 System.Console.WriteLine("CoOrds: {0},{1}", x, y);
             }

         }

         class TestCoOrds
        {
            static void Main()
            {
             CoOrds myCoOrds = new CoOrds(10, 15);
              myCoOrds.PrintCoOrds();
            }
       }
  
 A private: member of a class is accessible only by members and friends of the class.

See the following example code:

using System;


class Employee
{
    private string name = "FirstName, LastName";
    private double salary = 100.0;

    public string GetName()
    {
        return name;
    }

    public double Salary
    {
        get { return salary; }
    }
}

class MainClass
{
    static void Main()
    {
        Employee e = new Employee();

        // The data members are inaccessible (private), so
        // then can't be accessed like this:
        //    string n = e.name;
        //    double s = e.salary;

        // 'name' is indirectly accessed via method:
        string n = e.GetName();

        // 'salary' is indirectly accessed via property
        double s = e.Salary;
    }
}

 
A protected: member of a class is accessible by members and friends of the class and by members and friends of derived classes, provided they access the base member via a pointer or a reference to their own derived class.

please see the following example code:

 using System;
class A
{
    protected int x = 123;
}

class B : A
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        // Error CS1540, because x can only be accessed by
        // classes derived from A.
        // a.x = 10; 
        
        // OK, because this class derives from A.
        b.x = 10;   
    }
}
  A public: member of a class is accessible by everyone.

See the following example code:
using System;
class Point
{
    public int x; 
    public int y;
}

class MainClass
{
    static void Main() 
    {
        Point p = new Point();
        // Direct access to public members:
        p.x = 10;
        p.y = 15;
        Console.WriteLine("x = {0}, y = {1}", p.x, p.y); 
    }
}

 

 

A Property: Member is a generic term that refers to a field, property, or method of a class. A field is a variable, a property is a bit of data, and a method is a function/subroutine. For example:

public class BankAccount
{
    private int _balance;
    public int Balance
    {
        get { return _balance; }
        set { _balance = value; }
    }
    public void Deposit(int depositAmount)
    {
        // I can reference _balance here because I'm inside the class that defined it
        _balance = _balance + depositAmount;
    }
}

The above C# code represents a class. _balance (field), Balance (property), and Deposit (method) are all "members" of the class.
Think of a class as a blueprint. A blueprint isn't a house, it's the definition of what the house will look like when you build it. You "build" an object from a class by "new"ing it.

public class Bank
{
    public void Main()
    {
        // ba represents an object created from the class BankAccount
        BankAccount ba = new BankAccount();

        // This won't compile because _balance is private so it is not accessible outside the class
        ba._balance = 10;

        // This works fine because Balance is public, so it is accessible from this code that
        // exists outside of the class definition for BankAccount
        ba.Balance = 10;
    }
}