版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、.C# 基礎(chǔ)知識(shí) 25個(gè)常見問題 (1)2007-04-12 16:53當(dāng)初學(xué) C# 時(shí)是找個(gè)人大概問了一下數(shù)據(jù)類型和分支語句就開始做項(xiàng)目了。這兩天又全面的看了一下相關(guān)的基礎(chǔ)知識(shí)(學(xué)而時(shí)習(xí)之嘛),總結(jié)了25個(gè)問題: 1.靜態(tài)變量和非靜態(tài)變量的區(qū)別?2.const 和 static readonly 區(qū)別?3.extern 是什么意思?4.abstract 是什么意思?5.internal 修飾符起什么作用?6.sealed 修飾符是干什么的?7.override 和 overload 的區(qū)別?8.什么是索引指示器?9.new 修飾符是起什么作用?10.this 關(guān)鍵字的含義?11.可以使用抽象
2、函數(shù)重寫基類中的虛函數(shù)嗎?12.密封類可以有虛函數(shù)嗎?13.如果基類中的虛屬性只有一個(gè)屬性訪問器,那么繼承類重寫該屬性后可以有幾個(gè)屬性訪問器?如果基類中有 get 和 set 兩個(gè)呢?14.abstract 可以和 virtual 一起使用嗎?可以和 override 一起使用嗎?15.接口可以包含哪些成員?16.類和結(jié)構(gòu)的區(qū)別?17.接口的多繼承會(huì)帶來哪些問題?18.抽象類和接口的區(qū)別?19.別名指示符是什么?20.如何釋放非托管資源?21.P/Invoke是什么?22.StringBuilder 和 String 的區(qū)別?23.explicit 和 implicit 的含義?24.para
3、ms 有什么用?25.什么是反射? 以下是我做的一份參考答案(C# 語言范疇之內(nèi)),如果有不準(zhǔn)確、不全面的,歡迎各位朋友指正!1.靜態(tài)變量和非靜態(tài)變量的區(qū)別?答:靜態(tài)變量:靜態(tài)變量使用 static 修飾符進(jìn)行聲明在所屬類被裝載時(shí)創(chuàng)建通過類進(jìn)行訪問所屬類的所有實(shí)例的同一靜態(tài)變量都是同一個(gè)值非靜態(tài)變量:不帶有 static 修飾符聲明的變量稱做非靜態(tài)變量在類被實(shí)例化時(shí)創(chuàng)建通過對象進(jìn)行訪問同一個(gè)類的不同實(shí)例的同一非靜態(tài)變量可以是不同的值示例:using System;using System.Collections.Generic;using System.Text;namespace Examp
4、le01 class Program class Class1 public static String staticStr = Class; public String notstaticStr = Obj; static void Main(string args) /靜態(tài)變量通過類進(jìn)行訪問,該類所有實(shí)例的同一靜態(tài)變量都是同一個(gè)值 Console.WriteLine(Class1s staticStr: 0, Class1.staticStr); Class1 tmpObj1 = new Class1(); tmpObj1.notstaticStr = tmpObj1; Class1 tm
5、pObj2 = new Class1(); tmpObj2.notstaticStr = tmpObj2; /非靜態(tài)變量通過對象進(jìn)行訪問,不同對象的同一非靜態(tài)變量可以有不同的值 Console.WriteLine(tmpObj1s notstaticStr: 0, tmpObj1.notstaticStr); Console.WriteLine(tmpObj2s notstaticStr: 0, tmpObj2.notstaticStr); Console.ReadLine(); 結(jié)果:Class1s staticStr: ClasstmpObj1s notstaticStr: tmpObj1
6、tmpObj2s notstaticStr: tmpObj22.const 和 static readonly 區(qū)別?答: const用 const 修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶端程序static readonly用 static readonly 修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:通過類進(jìn)行訪問、初始化后不可以修改。但與常量不同的是這種變量是在運(yùn)行期初始化示例:測試類:using System;using System.Collections.Generic;using System.Text;namespace Example02Lib p
7、ublic class Class1 public const String strConst = Const; public static readonly String strStaticReadonly = StaticReadonly; /public const String strConst = Const Changed; /public static readonly String strStaticReadonly = StaticReadonly Changed; 客戶端代碼:using System;using System.Collections.Generic;usi
8、ng System.Text;using Example02Lib;namespace Example02 class Program static void Main(string args) /修改Example02中Class1的strConst初始值后,只編譯Example02Lib項(xiàng)目 /然后到資源管理器里把新編譯的Example02Lib.dll拷貝Example02.exe所在的目錄,執(zhí)行Example02.exe /切不可在IDE里直接調(diào)試運(yùn)行因?yàn)檫@會(huì)重新編譯整個(gè)解決方案! /可以看到strConst的輸出沒有改變,而strStaticReadonly的輸出已經(jīng)改變 /表明Co
9、nst變量是在編譯期初始化并嵌入到客戶端程序,而StaticReadonly是在運(yùn)行時(shí)初始化的 Console.WriteLine(strConst : 0, Class1.strConst); Console.WriteLine(strStaticReadonly : 0, Class1.strStaticReadonly); Console.ReadLine(); 結(jié)果:strConst : ConststrStaticReadonly : StaticReadonly 修改后的示例:測試類:using System;using System.Collections.Generic;usi
10、ng System.Text;namespace Example02Lib public class Class1 /public const String strConst = Const; /public static readonly String strStaticReadonly = StaticReadonly; public const String strConst = Const Changed; public static readonly String strStaticReadonly = StaticReadonly Changed; 結(jié)果strConst : Con
11、ststrStaticReadonly : StaticReadonly Changed3.extern 是什么意思?答: extern 修飾符用于聲明由程序集外部實(shí)現(xiàn)的成員函數(shù)經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用(通過 DllImport )。注意,和DllImport一起使用時(shí)要加上 static 修飾符也可以用于對于同一程序集不同版本組件的調(diào)用(用 extern 聲明別名)不能與 abstract 修飾符同時(shí)使用示例:using System;using System.Collections.Generic;using System.Text;using System.Runtime.Inter
12、opServices;namespace Example03 class Program /注意DllImport是一個(gè)Attribute Property,在System.Runtime.InteropServices命名空間中定義 /extern與DllImport一起使用時(shí)必須再加上一個(gè)static修飾符 DllImport(User32.dll) public static extern int MessageBox(int Handle, string Message, string Caption, int Type); static int Main() string myStr
13、ing; Console.Write(Enter your message: ); myString = Console.ReadLine(); return MessageBox(0, myString, My Message Box, 0); 結(jié)果:4.abstract 是什么意思?答: abstract 修飾符可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員abstract 不可以和 static 、virtual 、override 一起使用聲明為 abstract 成員可以不包括實(shí)現(xiàn)代碼,但只有類中還有未實(shí)現(xiàn)的抽象成員,該類就不可以被實(shí)例化,通常用于強(qiáng)制繼
14、承類必須實(shí)現(xiàn)某一成員示例:using System;using System.Collections.Generic;using System.Text;namespace Example04 #region 基類,抽象類 public abstract class BaseClass /抽象屬性,同時(shí)具有g(shù)et和set訪問器表示繼承類必須將該屬性實(shí)現(xiàn)為可讀寫 public abstract String Attribute get; set; /抽象方法,傳入一個(gè)字符串參數(shù)無返回值 public abstract void Function(String value); /抽象事件,類型為系
15、統(tǒng)預(yù)定義的代理(delegate):EventHandler public abstract event EventHandler Event; /抽象索引指示器,只具有g(shù)et訪問器表示繼承類必須將該索引指示器實(shí)現(xiàn)為只讀 public abstract Char thisint Index get; #endregion #region 繼承類 public class DeriveClass : BaseClass private String attribute; public override String Attribute get return attribute; set attr
16、ibute = value; public override void Function(String value) attribute = value; if (Event != null) Event(this, new EventArgs(); public override event EventHandler Event; public override Char thisint Index get return attributeIndex; #endregion class Program static void OnFunction(object sender, EventArgs e) for (int i = 0; i (DeriveClass)sender).Attribute.Length; i+) Console.WriteLine(DeriveClass)sender)i); static void Main(string ar
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 植物胚胎發(fā)育信號(hào)傳導(dǎo)-深度研究
- 2025至2030年中國中心信號(hào)源系統(tǒng)數(shù)據(jù)監(jiān)測研究報(bào)告
- 2025年中國五滾珠脹頭市場調(diào)查研究報(bào)告
- 二零二五年度會(huì)展中心場地租賃協(xié)議11篇
- 二零二五年度促銷員離職補(bǔ)償合同3篇
- 二零二五年度購物中心裝修承包合同安全責(zé)任協(xié)議
- 二零二五年度贍養(yǎng)老人生活照料及費(fèi)用分?jǐn)倕f(xié)議范本
- 二零二五年度足浴店與知名品牌聯(lián)名活動(dòng)合作協(xié)議
- 二零二五年度車輛抵押個(gè)人汽車保險(xiǎn)貸款合同模板
- 二零二五年度辣椒種植與農(nóng)產(chǎn)品出口貿(mào)易合同
- 2024屆上海高考語文課內(nèi)古詩文背誦默寫篇目(精校版)
- DL-T5024-2020電力工程地基處理技術(shù)規(guī)程
- 2024年度-美團(tuán)新騎手入門培訓(xùn)
- 初中數(shù)學(xué)要背誦記憶知識(shí)點(diǎn)(概念+公式)
- 駕照體檢表完整版本
- 農(nóng)產(chǎn)品農(nóng)藥殘留檢測及風(fēng)險(xiǎn)評估
- 農(nóng)村高中思想政治課時(shí)政教育研究的中期報(bào)告
- 20100927-宣化上人《愣嚴(yán)咒句偈疏解》(簡體全)
- 4-熔化焊與熱切割作業(yè)基礎(chǔ)知識(shí)(一)
- 單元教學(xué)評一體化設(shè)計(jì)的探索與實(shí)踐以統(tǒng)編語文教材四年級下冊第一單元為例
- 醫(yī)院標(biāo)識(shí)牌方案設(shè)計(jì)2
評論
0/150
提交評論