【記錄】c# 基礎知識複習

來源:互聯網
上載者:User

A:internal 修飾符起什麼作用?

internal 修飾符可以用於類型或成員,使用該修飾符聲明的類型或成員只能在同一程集內提供者的成員不能使用 internal 修飾符值得注意的是,如果為 internal 成員加上了 protected 修飾符,這時的存取層級為 internal 或 protected。只是看字面意思容易弄錯,許多人認為 internal protected 應該是“只有同一個程式集中的子類可以訪問”,但其實它表示“同一個程式集中的所有類,以及所有程式集中的子類都可以訪問”

樣本

Example05Lib 項目的 Class1 

 

Codeusing System;using System.Collections.Generic;using System.Text; namespace Example05Lib{    public class Class1    {        internal String strInternal = null;        public String strPublic;        internal protected String strInternalProtected = null;    }}

 

結果
Example05Lib 項目的 Class2 類可以訪問到 Class1 的 strInternal 成員,當然也可以訪問到 strInternalProtected 成員,因為他們在同一個程式集裡

Example05 項目裡的 Class3 類無法訪問到 Class1 的 strInternal 成員,因為它們不在同一個程式集裡。但卻可以訪問到 strInternalProtected 成員,因為 Class3 是 Class1 的繼承類

Example05 項目的 Program 類既無法訪問到 Class1 的 strInternal 成員,也無法訪問到 strInternalProtected 成員,因為它們既不在同一個程式集裡也不存在繼承關係B

B:欄位和屬性問題

欄位:與類或對象相關聯的變數,一般訪問域為private屬性:欄位的封裝,通過訪問器(accessor)來進行讀寫操作,一般為public
屬性是類提供給外部調用時用,的可以使用 對象.屬性 設定或讀取 一個值get{reture xxx;}表示可讀set{xxx = value;}表示可寫欄位就是類內部用的.儲存資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.