C#基礎(chǔ)概念學(xué)習(xí)筆記
C#基礎(chǔ)概念之extern 是什么意思?
extern 修飾符用于聲明由程序集外部實現(xiàn)的成員函數(shù),經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用(通過 DllImport )。注意,和DllImport一起使用時要加上 static 修飾符,也可以用于對于同一程序集不同版本組件的調(diào)用(用 extern 聲明別名),不能與 abstract 修飾符同時使用。
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace Example03 {
- class Program {
- //注意DllImport是一個Attribute Property,
在System.Runtime.InteropServices命名空間中定義- //extern與DllImport一起使用時必須再加上一個static修飾符[DllImport("User32.dll")]
- public static extern int MessageBox
(int Handle, string Message, string Caption, int Type);- static int Main(){
- string myString;
- Console.Write("Enter your message: ");
- myString = Console.ReadLine();
- return MessageBox(0, myString, "My Message Box", 0);
- }
C#基礎(chǔ)概念之a(chǎn)bstract 是什么意思?
abstract 修飾符可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員,abstract 不可以和 static 、virtual 、override 一起使用,聲明為 abstract 成員可以不包括實現(xiàn)代碼,但只有類中還有未實現(xiàn)的抽象成員,該類就不可以被實例化,通常用于強制繼承類必須實現(xiàn)某一成員
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Example04 {
- public abstract class BaseClass {
- //抽象屬性,同時具有g(shù)et和set訪問器表示繼承類必須將該屬性實現(xiàn)為可讀寫
- public abstract String Attribute {
- get;
- set;
- }
- //抽象方法,傳入一個字符串參數(shù)無返回值
- public abstract void Function(String value);
- //抽象事件,類型為系統(tǒng)預(yù)定義的代理(delegate):
- EventHandler public abstract event EventHandler Event;
- //抽象索引指示器,只具有g(shù)et訪問器表示繼承類必須將該索引指示器實現(xiàn)為只讀
- public abstract Char this[int Index] {
- get;
- }
- public class DeriveClass : BaseClass {
- private String attribute;
- public override String Attribute {
- get {
- return attribute;
- }
- set {
- attribute = value;
- }
- public override void Function(String value){
- attribute = value;
- if (Event != null){
- Event(this, new EventArgs());
- }
- public override event EventHandler Event;
- public override Char this[int Index] {
- get {
- return attribute[Index];
- }
- 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[] args){
- DeriveClass tmpObj = new DeriveClass();
- tmpObj.Attribute = "1234567";Console.WriteLine(tmpObj.Attribute);
- //將靜態(tài)函數(shù)OnFunction與tmpObj對象的Event事件進行關(guān)聯(lián)
- tmpObj.Event += new EventHandler(OnFunction);
- tmpObj.Function("7654321");
- Console.ReadLine();
- }
C#基礎(chǔ)概念之internal 修飾符起什么作用?
internal 修飾符可以用于類型或成員,使用該修飾符聲明的類型或成員只能在同一程集內(nèi)訪問,接口的成員不能使用 internal 修飾符
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Example05Lib {
- public class Class1 {
- internal String strInternal = null;
- public String strPublic;
- }
【編輯推薦】