• Private Protect Partial Internal Public 区别


    摘自: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;
        }
    }

  • 相关阅读:
    第二十八节:Asp.Net Core中JWT的几种写法和认证方式
    第二十七节:安全存储机密程序
    第二十六节:扩展如何在控制台中使用HttpClientFactory、读取配置文件、数据保护、注入类
    第二十五节:数据保护程序和Hash的最佳实现(彩虹表原理)
    第二十四节:编码、解码、加密算法概念及实现(Base64、MD5、SHA、HMAC、DES、AES、RSA)
    第二十三节:Asp.Net Core中的几种安全防护
    第二十二节:Asp.Net Core中Https协议的相关配置
    第二十一节:Asp.Net Core中使用托管服务实现后台任务
    第X节:抢红包算法分享
    第四节:Geo类型介绍以及Redis批量操作、事务、分布式锁
  • 原文地址:https://www.cnblogs.com/FredTang/p/1680602.html
Copyright © 2020-2023  润新知