C#基础概念学习笔记

C#基础概念之extern 是什么意思?

创新互联公司长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为双清企业提供专业的成都做网站、成都网站制作,双清网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

extern 修饰符用于声明由程序集外部实现的成员函数,经常用于系统API函数的调用(通过 DllImport )。注意,和DllImport一起使用时要加上 static 修饰符,也可以用于对于同一程序集不同版本组件的调用(用 extern 声明别名),不能与 abstract 修饰符同时使用。

示例:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5.  
  6. namespace Example03 {   
  7. class Program {   
  8. //注意DllImport是一个Attribute Property,
    在System.Runtime.InteropServices命名空间中定义  
  9. //extern与DllImport一起使用时必须再加上一个static修饰符[DllImport("User32.dll")]   
  10. public static extern int MessageBox
    (int Handle, string Message, string Caption, int Type);  
  11.  
  12. static int Main(){   
  13. string myString;  
  14. Console.Write("Enter your message: ");  
  15. myString = Console.ReadLine();  
  16. return MessageBox(0, myString, "My Message Box", 0);  

C#基础概念之abstract 是什么意思?

abstract 修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽象成员,abstract 不可以和 static 、virtual 、override 一起使用,声明为 abstract 成员可以不包括实现代码,但只有类中还有未实现的抽象成员,该类就不可以被实例化,通常用于强制继承类必须实现某一成员

示例:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace Example04 {   
  6.  
  7. public abstract class BaseClass {   
  8. //抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写  
  9. public abstract String Attribute {   
  10. get;  
  11. set;  
  12. }  
  13.  
  14. //抽象方法,传入一个字符串参数无返回值  
  15. public abstract void Function(String value);  
  16.  
  17. //抽象事件,类型为系统预定义的代理(delegate):  
  18. EventHandler public abstract event EventHandler Event;  
  19.  
  20. //抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读  
  21. public abstract Char this[int Index] {   
  22. get;  
  23. }   
  24.  
  25. public class DeriveClass : BaseClass {   
  26. private String attribute;  
  27.  
  28. public override String Attribute {   
  29. get {   
  30. return attribute;  
  31. }   
  32. set {   
  33. attribute = value;  
  34. }   
  35. public override void Function(String value){   
  36. attribute = value;  
  37. if (Event != null){   
  38. Event(this, new EventArgs());  
  39. }   
  40. public override event EventHandler Event;  
  41. public override Char this[int Index] {   
  42. get {   
  43. return attribute[Index];  
  44. }   
  45.  
  46. class Program { static void OnFunction(object sender, EventArgs e){   
  47. for (int i = 0;   
  48. < ((DeriveClass)sender)。Attribute.Length;   
  49. i++){ Console.WriteLine(((DeriveClass)sender)[i]);  
  50. }   
  51. static void Main(string[] args){   
  52. DeriveClass tmpObj = new DeriveClass();  
  53. tmpObj.Attribute = "1234567";Console.WriteLine(tmpObj.Attribute);  
  54. //将静态函数OnFunction与tmpObj对象的Event事件进行关联  
  55. tmpObj.Event += new EventHandler(OnFunction);  
  56. tmpObj.Function("7654321");  
  57. Console.ReadLine();  

C#基础概念之internal 修饰符起什么作用?

internal 修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问,接口的成员不能使用 internal 修饰符

示例:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace Example05Lib {   
  6. public class Class1 {   
  7. internal String strInternal = null;  
  8. public String strPublic;  

网站题目:C#基础概念学习笔记
网站URL:http://www.hantingmc.com/qtweb/news46/243146.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联