你必须知道的Csharp的25个基础概念.docx

上传人:b****5 文档编号:3278288 上传时间:2022-11-21 格式:DOCX 页数:40 大小:28.98KB
下载 相关 举报
你必须知道的Csharp的25个基础概念.docx_第1页
第1页 / 共40页
你必须知道的Csharp的25个基础概念.docx_第2页
第2页 / 共40页
你必须知道的Csharp的25个基础概念.docx_第3页
第3页 / 共40页
你必须知道的Csharp的25个基础概念.docx_第4页
第4页 / 共40页
你必须知道的Csharp的25个基础概念.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

你必须知道的Csharp的25个基础概念.docx

《你必须知道的Csharp的25个基础概念.docx》由会员分享,可在线阅读,更多相关《你必须知道的Csharp的25个基础概念.docx(40页珍藏版)》请在冰豆网上搜索。

你必须知道的Csharp的25个基础概念.docx

你必须知道的Csharp的25个基础概念

1.静态变量和非静态变量的区别?

2.const和staticreadonly区别?

3.extern是什么意思?

4.abstract是什么意思?

5.internal修饰符起什么作用?

6.sealed修饰符是干什么的?

7.override和overload的区别?

8.什么是索引指示器?

9.new修饰符是起什么作用?

10.this关键字的含义?

11.可以使用抽象函数重写基类中的虚函数吗?

12.密封类可以有虚函数吗?

13.如果基类中的虚属性只有一个属性访问器,那么继承类重写该属性后可以有几个属性访问器?

如果基类中有get和set两个呢?

14.abstract可以和virtual一起使用吗?

可以和override一起使用吗?

15.接口可以包含哪些成员?

16.类和结构的区别?

17.接口的多继承会带来哪些问题?

18.抽象类和接口的区别?

19.别名指示符是什么?

20.如何释放非托管资源?

21.P/Invoke是什么?

22.StringBuilder和String的区别?

23.explicit和implicit的含义?

24.params有什么用?

25.什么是反射?

1.静态变量和非静态变量的区别?

答:

静态变量:

静态变量使用static修饰符进行声明在所属类被装载时创建通过类进行访问所属类的所有实例的同一静态变量都是同一个值非静态变量:

不带有static修饰符声明的变量称做非静态变量在类被实例化时创建通过对象进行访问同一个类的不同实例的同一非静态变量可以是不同的值示例:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample01

{

classProgram

{

classClass1

{

publicstaticStringstaticStr="Class";

publicStringnotstaticStr="Obj";

}

staticvoidMain(string[]args)

{

//静态变量通过类进行访问,该类所有实例的同一静态变量都是同一个值

Console.WriteLine("Class1'sstaticStr:

{0}",Class1.staticStr);

Class1tmpObj1=newClass1();

tmpObj1.notstaticStr="tmpObj1";

Class1tmpObj2=newClass1();

tmpObj2.notstaticStr="tmpObj2";

//非静态变量通过对象进行访问,不同对象的同一非静态变量可以有不同的值

Console.WriteLine("tmpObj1'snotstaticStr:

{0}",tmpObj1.notstaticStr);

Console.WriteLine("tmpObj2'snotstaticStr:

{0}",tmpObj2.notstaticStr);

Console.ReadLine();

}

}

}

复制代码

结果:

Class1'sstaticStr:

ClasstmpObj1'snotstaticStr:

tmpObj1tmpObj2'snotstaticStr:

tmpObj2

2.const和staticreadonly区别?

答:

const用const修饰符声明的成员叫常量,是在编译期初始化并嵌入到客户端程序staticreadonly用staticreadonly修饰符声明的成员依然是变量,只不过具有和常量类似的使用方法:

通过类进行访问、初始化后不可以修改。

但与常量不同的是这种变量是在运行期初始化示例:

测试类:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample02Lib

{

publicclassClass1

{

publicconstStringstrConst="Const";

publicstaticreadonlyStringstrStaticReadonly="StaticReadonly";

//publicconstStringstrConst="ConstChanged";

//publicstaticreadonlyStringstrStaticReadonly="StaticReadonlyChanged";

}//5-1-a-s-p-x

}

复制代码

客户端代码:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingExample02Lib;

namespaceExample02

{

classProgram

{

staticvoidMain(string[]args)

{

//修改Example02中Class1的strConst初始值后,只编译Example02Lib项目

//然后到资源管理器里把新编译的Example02Lib.dll拷贝Example02.exe所在的目录,执行Example02.exe

//切不可在IDE里直接调试运行因为这会重新编译整个解决方案!

//可以看到strConst的输出没有改变,而strStaticReadonly的输出已经改变

//表明Const变量是在编译期初始化并嵌入到客户端程序,而StaticReadonly是在运行时初始化的

Console.WriteLine("strConst:

{0}",Class1.strConst);

Console.WriteLine("strStaticReadonly:

{0}",Class1.strStaticReadonly);

Console.ReadLine();

}

}

}

复制代码

结果:

strConst:

ConststrStaticReadonly:

StaticReadonly修改后的示例:

测试类:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample02Lib

{

publicclassClass1

{

//publicconstStringstrConst="Const";

//publicstaticreadonlyStringstrStaticReadonly="StaticReadonly";

publicconstStringstrConst="ConstChanged";

publicstaticreadonlyStringstrStaticReadonly="StaticReadonlyChanged";

}

}

复制代码

结果strConst:

ConststrStaticReadonly:

StaticReadonlyChanged

3.extern是什么意思?

答:

extern修饰符用于声明由程序集外部实现的成员函数经常用于系统API函数的调用(通过DllImport)。

注意,和DllImport一起使用时要加上static修饰符也可以用于对于同一程序集不同版本组件的调用(用extern声明别名)不能与abstract修饰符同时使用51aspx示例:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Runtime.InteropServices;

namespaceExample03

{

classProgram

{

//注意DllImport是一个AttributeProperty,在System.Runtime.InteropServices命名空间中定义

//extern与DllImport一起使用时必须再加上一个static修饰符

[DllImport("User32.dll")]

publicstaticexternintMessageBox(intHandle,stringMessage,stringCaption,intType);

staticintMain()

{

stringmyString;

Console.Write("Enteryourmessage:

");

myString=Console.ReadLine();

returnMessageBox(0,myString,"MyMessageBox",0);

}

}

}

复制代码

结果:

4.abstract是什么意思?

答:

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

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample04

{

#region基类,抽象类

publicabstractclassBaseClass

{

//抽象属性,同时具有get和set访问器表示继承类必须将该属性实现为可读写

publicabstractStringAttribute

{

get;

set;

}

//抽象方法,传入一个字符串参数无返回值

publicabstractvoidFunction(Stringvalue);

//抽象事件,类型为系统预定义的代理(delegate):

EventHandler

publicabstracteventEventHandlerEvent;

//抽象索引指示器,只具有get访问器表示继承类必须将该索引指示器实现为只读

publicabstractCharthis[intIndex]

{

get;

}

}

#endregion

#region继承类

publicclassDeriveClass:

BaseClass

{

privateStringattribute;

publicoverrideStringAttribute

{

get

{

returnattribute;

}

set

{

attribute=value;

}

}

publicoverridevoidFunction(Stringvalue)

{

attribute=value;

if(Event!

=null)

{

Event(this,newEventArgs());

}

}

publicoverrideeventEventHandlerEvent;

publicoverrideCharthis[intIndex]

{

get

{

returnattribute[Index];

}

}

}

#endregion

classProgram

{

staticvoidOnFunction(objectsender,EventArgse)

{

for(inti=0;i<((DeriveClass)sender).Attribute.Length;i++)

{

Console.WriteLine(((DeriveClass)sender)[i]);

}

}

staticvoidMain(string[]args)

{

DeriveClasstmpObj=newDeriveClass();

tmpObj.Attribute=&quot;1234567&quot;;

Console.WriteLine(tmpObj.Attribute);

//将静态函数OnFunction与tmpObj对象的Event事件进行关联

tmpObj.Event+=newEventHandler(OnFunction);

tmpObj.Function(&quot;7654321&quot;);

Console.ReadLine();

}

}

}

复制代码

结果:

12345677654321

5.internal修饰符起什么作用?

答:

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

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample05Lib

{

publicclassClass1

{

internalStringstrInternal=null;

publicStringstrPublic;

}

}

复制代码

结果Example05Lib项目的Class2类可以访问到Class1的strInternal成员Example05项目的Program类无法访问到Class1的strInternal成员

6.sealed修饰符是干什么的?

答:

sealed修饰符表示密封用于类时,表示该类不能再被继承,不能和abstract同时使用,因为这两个修饰符在含义上互相排斥用于方法和属性时,表示该方法或属性不能再被继承,必须和override关键字一起使用,因为使用sealed修饰符的方法或属性肯定是基类中相应的虚成员通常用于实现第三方类库时不想被客户端继承,或用于没有必要再继承的类以防止滥用继承造成层次结构体系混乱恰当的利用sealed修饰符也可以提高一定的运行效率,因为不用考虑继承类会重写该成员示例:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample06

{

classProgram

{

classA

{

publicvirtualvoidF()

{

Console.WriteLine(&quot;A.F&quot;);

}

publicvirtualvoidG()

{

Console.WriteLine(&quot;A.G&quot;);

}

}

classB:

A

{

publicsealedoverridevoidF()

{

Console.WriteLine(&quot;B.F&quot;);

}

publicoverridevoidG()

{

Console.WriteLine(&quot;B.G&quot;);

}

}

classC:

B

{

publicoverridevoidG()

{

Console.WriteLine(&quot;C.G&quot;);

}

}

staticvoidMain(string[]args)

{

newA().F();

newA().G();

newB().F();

newB().G();

newC().F();

newC().G();

Console.ReadLine();

}

}

}

复制代码

结果:

类B在继承类A时可以重写两个虚函数,如图所示:

由于类B中对F方法进行了密封,类C在继承类B时只能重写一个函数,如图所示:

控制台输出结果,类C的方法F只能是输出类B中对该方法的实现:

A.FA.GB.FB.GB.FC.G

7.override和overload的区别?

答:

override表示重写,用于继承类对基类中虚成员的实现overload表示重载,用于同一个类中同名方法不同参数(包括类型不同或个数不同)的实现示例:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample07

{

classProgram

{

classBaseClass

{

publicvirtualvoidF()

{

Console.WriteLine(&quot;BaseClass.F&quot;);

}

}

classDeriveClass:

BaseClass

{

publicoverridevoidF()

{

base.F();

Console.WriteLine(&quot;DeriveClass.F&quot;);

}

publicvoidAdd(intLeft,intRight)

{

Console.WriteLine(&quot;AddforInt:

{0}&quot;,Left+Right);

}

publicvoidAdd(doubleLeft,doubleRight)

{

Console.WriteLine(&quot;Addforint:

{0}&quot;,Left+Right);

}

}

staticvoidMain(string[]args)

{

DeriveClasstmpObj=newDeriveClass();

tmpObj.F();

tmpObj.Add(1,2);

tmpObj.Add(1.1,2.2);

Console.ReadLine();

}

}

}

复制代码

结果:

BaseClass.FDeriveClass.FAddforInt:

3Addforint:

3.38.什么是索引指示器?

答:

实现索引指示器(indexer)的类可以象数组那样使用其实例后的对象,但与数组不同的是索引指示器的参数类型不仅限于int简单来说,其本质就是一个含参数属性示例:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceExample08

{

publicclassPoint

{

privatedoublex,y;

publicPoint(doubleX,doubleY)

{

x=X;

y=Y;

}

//重写ToString方法方便输出

publicoverridestringToString()

{

returnString.Format(&quot;X:

{0},Y:

{1}&quot;,x,y);

}

}

publicclassPoints

{

Point[]points;

publicPoints(Point[]Points)

{

points=Points;

}

publicintPointNumber

{

get

{

returnpoints.Length;

}

}

//实现索引访问器

publicPointthis[intIndex]

{

get

{

returnpoints[Index];

}

}

}

//感谢watsonhua(

//索引指示器的实质是含参属性,参数并不只限于int

classWeatherOfWeek

{

publicstringthis[intIndex]

{

get

{

//注意case段使用return直接返回所以不需要break

switch(Index)

{

case0:

{

return&quot;Todayiscloudy!

&quot;;

}

case5:

{

return&quot;Todayisthundershower!

&quot;;

}

default:

{

return&quot;Todayisfine!

&quot;;

}

}

}

}

publicstringthis[stringDay]

{

get

{

stringTodayWeather=null;

//switch的标准写法

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1