⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 9.mht

📁 C# Nuts and Bolt是学习C#的极好教程
💻 MHT
📖 第 1 页 / 共 5 页
字号:
<P class=3DCoutput>yyy<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">typeof expects the =
name of a=20
class as a parameter. C# allows a variable name and a class name to be =
the same.=20
At the line s =3D yyy, C# assumes yyy to be the name of a variable. yyy =
is=20
considered to be a class name and t is initialised to the typeof =
returned by it.=20
Thus, C# uses a lot of intelligence to read between the lines.=20
<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Our resident =
astrologer informed=20
us that at least 1.54234 per cent of our book must be copied from the=20
documentation. To make sure that no ill luck befalls us, we copied a =
couple of=20
lines from it. Thus an sbyte is an alias for System.Sbyte,byte =
System.Byte,=20
short System.Int16, ushort System.UInt16, uint System.UInt32, long =
System.Int64,=20
ulong System.UInt64, char System.Char, float System.Single, double=20
System.Double, decimal System.Decimal, and, finally, bool =
System.Boolean. Enums=20
are simple derived from System.Enum and are simple constants. Object is =
an alias=20
for System.Object and string System.String. Interfaces are only derived =
from=20
object and arrays form System.Array. Delegates are from System.Delegate. =
These=20
have been mentioned earlier, they have been placed here only as a ready=20
reckner.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: Tahoma">Member =

Lookup<o:p></o:p></SPAN></B></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">A member lookup is a =
way by means=20
of which the meaning of a name in a type is determined or figured out. A =
class=20
can have base classes and all classes have one base class object. The =
compiler=20
first figures out all the names that match in the class and base class. =
We can=20
have a constant and a property with the same name in a base class and =
derived=20
class. We mentioned this earlier, but it merits repeating. If we ever =
have an=20
override modifier in front of a member name, then this name has =
overridden the=20
earlier name in the base class. It is thus removed from the list as it =
is a=20
different name. If the name is a constant, field, property, event, type =
or enum,=20
it hides all the members of the base class. If it is function, then all=20
non-methods are hidden in the base class. The end result could either be =
a=20
single non-method name or a series of methods. Anything else is an =
error.=20
<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Every known entity is =
derived=20
from object. We have mentioned this at least a hundred times already. =
Value=20
types cannot derive from any other type except object unlike classes and =

interfaces. However, arrays and delegates are derived from System.Array =
and=20
System.Delegate respectively. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: Tahoma">Name=20
Resolution<o:p></o:p></SPAN></B></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">After the giving a =
namespace and=20
a period, we can either use the name of another namespace or the name of =
a type.=20
If we start, however, with the name of a type, then what follows can be =
one of=20
the following. Either a type as in a nested types, or a method, a static =

property, static readonly field or a static field, static event, =
constant, or=20
finally an enum. Anything else signals an error. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>class zzz<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>aaa aaa;<o:p></o:p></P>
<P class=3DCprg>aaa bbb =3D aaa.bbb();<o:p></o:p></P>
<P class=3DCprg>aaa ccc =3D aaa.ccc;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class aaa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static aaa ccc =3D new aaa();<o:p></o:p></P>
<P class=3DCprg>public static aaa bbb()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return new aaa();<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">If you want to =
confuse any person=20
using your code, use the above code. The above explanation clearly =
states that=20
we can create a constant, field, local variable or a parameter with the =
same=20
name as the name of a type. Thus, we have a class aaa which contains two =

members. One is a static field called ccc and<SPAN=20
style=3D"mso-spacerun: yes">&nbsp; </SPAN>the other is a static function =
bbb which=20
returns an aaa type. In Main, we create a variable called aaa, same as =
the data=20
type. Then we call static members of the class aaa using the name of the =
type.=20
All this leeway is given to us because we are allowed access to static =
members=20
of the class aaa without any ambiguity.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">An invocation =
expression can=20
either be a method group or a delegate. Nothing else can be executable. =
If the=20
function returns void, the result is nothing. Thus, an expression which =
results=20
in nothing cannot obviously be the operand of a operator, it can only be =
used a=20
statement. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">A method group can =
either be one=20
method or a group of methods. The parameter types decide which of the =
methods=20
would be chosen. To execute a certain method, the compiler starts with =
the type=20
before the method. It then proceeds up the inheritance chain and finds =
an=20
applicable, accessible, non-override method. If it finds more than one, =
it uses=20
overload resolution to decide the best one. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: =
Tahoma">Constructor=20
Signatures<o:p></o:p></SPAN></B></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int i =3D 3;<o:p></o:p></P>
<P class=3DCprg>yyy a =3D new yyy(ref i);<o:p></o:p></P>
<P class=3DCprg>int j;<o:p></o:p></P>
<P class=3DCprg>yyy b =3D new yyy(out j);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i + " " + j);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public yyy(ref int p)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>p =3D 100;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public yyy(out int q)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>q =3D 1000;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCoutput><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(18,8): error CS0663: '.ctor' cannot define =
overloaded=20
methods which differ only on ref and out<o:p></o:p></P>
<P class=3DCoutput>a.cs(14,8): (Location of symbol related to previous=20
error)<o:p></o:p></P>
<P class=3DCbase><SPAN=20
style=3D"COLOR: red"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase>A constructor cannot define overloaded methods =
differing only on=20
ref or out. The above logic makes sense as a constructor is nothing but =
a=20
function that is called automatically at birth. It is in no way =
different from=20
any other functions that we have worked with.<SPAN=20
style=3D"COLOR: windowtext"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>yyy a =3D new yyy(1,2,3);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public yyy(params int [] q)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>foreach ( int i in q)<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + " " );<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>1 2 3<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">We can use the params =
modifier to=20
our hearts content in displaying the parameters to the=20
constructors.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U> <o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>yyy a =3D new yyy(1,2,3);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public yyy(params int [] q)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>foreach ( int i in q)<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + " " );<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public yyy ( int i, int j , int k)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.Write("int");<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>int<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">However, the params =
modifier is=20
given the last priority by the compiler. If there is an exact match, a=20
one-to-one with the parameters in the constructors, the compiler gives=20
precedence to it and the params is ignored for good. For three ints, the =

constructor with three parameters will be called and for any other =
combination=20
of ints, the params constructor will be called.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>yyy a =3D new yyy(1,2,3);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public yyy(int p, int p1,int p2,params int [] =
q)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -