C#基础概念总结

开发 后端
本文介绍C#基础概念,包括接口的多继承会带来哪些问题、抽象类和接口的区别和别名指示符是什么等方面的介绍。

C#基础概念之接口的多继承会带来哪些问题?

C# 中的接口与类不同,可以使用多继承,即一个子接口可以有多个父接口。但如果两个父成员具有同名的成员,就产生了二义性(这也正是 C# 中类取消了多继承的原因之一),这时在实现时最好使用显式的声明

示例:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace Example17 {   
  6. class Program {   
  7. //一个完整的接口声明示例interface IExample {   
  8. //属性string P { get;set;  
  9. }   
  10. //方法string F(int Value);  
  11. //事件event EventHandler E;  
  12. //索引指示器string this[int Index] {   
  13. get;set;  
  14. }   
  15. interface IA {   
  16. int Count {   
  17. get; set;  
  18. }   
  19. }   
  20. interface IB {   
  21. int Count();  
  22. }   
  23. //IC接口从IA和IB多重继承interface IC : IA, IB {   
  24. }   
  25. class C : IC {   
  26. private int count = 100;  
  27. //显式声明实现IA接口中的Count属性int IA.Count {   
  28. get {  
  29. return 100;   
  30. }   
  31. set { count = value; }   
  32. }   
  33. //显式声明实现IB接口中的Count方法int IB.Count(){   
  34. return count * count;} static void Main(string[] args){   
  35. tmpObj = new C();  
  36. //调用时也要显式转换Console.WriteLine("Count property: {0}",((IA)tmpObj)。Count);  
  37. Console.WriteLine("Count function: {0}",((IB)tmpObj)。Count());  
  38.  
  39. Console.ReadLine();  

C#基础概念之抽象类和接口的区别?

抽象类(abstract class)可以包含功能定义和实现,接口(interface)只能包含功能定义,抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性,分析对象,提炼内部共性形成抽象类,用以表示对象本质,即“是什么”,为外部提供调用或功能需要扩充时优先使用接口

C#基础概念之别名指示符是什么?

通过别名指示符我们可以为某个类型起一个别名,主要用于解决两个命名空间内有同名类型的冲突或避免使用冗余的命名空间,别名指示符只在一个单元文件内起作用

示例:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01 {   
  6. class Class1 {   
  7. public override string ToString(){   
  8. return "com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01's Class1";  
  9. }   
  10. Class2.cs  
  11.  
  12. using System;  
  13. using System.Collections.Generic;  
  14. using System.Text;  
  15.  
  16. namespace com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02 {   
  17. class Class1 {   
  18. public override string ToString(){   
  19. return "com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02's Class1";  
  20. }主单元(Program.cs):  
  21.  
  22. using System;  
  23. using System.Collections.Generic;  
  24. using System.Text;  
  25.  
  26. //使用别名指示符解决同名类型的冲突  
  27. using Lib01Class1 = com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01.Class1;  
  28. using Lib02Class2 = com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02.Class1;  
  29.  
  30. namespace Example19 {   
  31. class Program { static void Main(string[] args){   
  32. Lib01Class1 tmpObj1 = new Lib01Class1();  
  33. Lib02Class2 tmpObj2 = new Lib02Class2();  
  34.  
  35. Console.WriteLine(tmpObj1);  
  36. Console.WriteLine(tmpObj2);  
  37. Console.ReadLine();  

【编辑推荐】

  1. C#反射方法学习总结
  2. 浅谈C#测量cpu性能
  3. C#远程计算机的一些理论知识
  4. 浅析C# Static修饰
  5. C#转换农历的简单方法
责任编辑:佚名 来源: 51CTO.com
相关推荐

2009-08-13 10:52:03

C#基础概念

2009-09-17 17:44:51

C#动态数组

2011-04-25 09:22:44

C#事件

2011-04-22 09:14:26

C#委托

2011-04-26 08:56:31

C#

2011-06-08 14:22:51

延迟加载

2009-08-13 11:10:32

C#基础概念

2009-08-13 12:50:45

C#基础知识

2009-08-27 16:37:06

C#基础知识

2009-08-27 10:14:04

LINQ基础概念

2009-08-19 17:13:15

C# 操作符基础知识

2009-09-02 10:58:02

C#动态数组

2009-08-25 17:49:07

C#入门

2009-09-14 13:44:14

Lambda ExprC# Lambda

2009-08-27 16:54:59

C#开发技巧

2009-08-11 15:44:05

C#基本技巧

2009-08-13 18:21:52

C#学习笔记

2009-08-17 17:04:29

C#转义字符

2009-08-25 10:19:09

C# Lazy-All

2009-08-28 13:12:56

C#反射实例C#反射
点赞
收藏

51CTO技术栈公众号