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

📄 chapter 9.mht

📁 C# Nuts and Bolt是学习C#的极好教程
💻 MHT
📖 第 1 页 / 共 5 页
字号:
<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">Regardless of both =
the=20
constructors matching very closely, the compiler yet does not give us =
any error.=20
This is because it treats the params as a second class citizen of the =
world. We=20
would never like to be in the shoes of a params =
parameter.<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">This=20
<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">This is only =
permitted to be used=20
in three places namely a constructor, instance method and an instance =
accessor.=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=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>yyy a =3D new yyy();<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()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(this);<o:p></o:p></P>
<P class=3DCprg>if ( this is yyy)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yes yyy");<o:p></o:p></P>
<P class=3DCprg>if ( this is zzz)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yes zzz");<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public static implicit operator string (yyy =
a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return "hi";<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>Compiler Warning</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(13,6): warning CS0183: The given expression is =
always of=20
the provided ('yyy') type<o:p></o:p></P>
<P class=3DCoutput>a.cs(15,6): warning CS0184: The given expression is =
never of=20
the provided ('zzz') type<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>hi<o:p></o:p></P>
<P class=3DCoutput>yes 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">A this in a =
constructor is=20
considered to be the value of the data type of the class that contains =
the=20
constructor. In the above program, the is operator is used to reconfirm =
the=20
above statement. Also, the implicit string operator is called as the =
this has to=20
be converted to a string for WriteLine function. Ditto for instance =
members and=20
accessors. Comment out the string operator and the output will be=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=3DCoutput>yyy<o:p></o:p></P>
<P class=3DCoutput>yes 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">For a constructor in =
a structure,=20
the rules change dramatically. Here it is a variable. Like earlier, it =
stands=20
for a reference to the structure it is placed in, but a major difference =
is that=20
it is classified as an out parameter. Thus, all the members of the =
structure=20
must be initialized before we leave the constructor. =
<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">In the case of an =
instance=20
method, everything remains the same as above, but now it behaves as a =
ref=20
parameter instead of an out. Being a ref parameter, someone else is =
responsible=20
for initializing the variable. The function can access all the instance=20
variables in the structure and also infringe the benefit of changing =
them if we=20
so choose to. A this is not used on anything remotely connected with =
static or=20
in variable initializer of a field declaration. A program relating to =
this is as=20
follows.<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>int j =3D 10;<o:p></o:p></P>
<P class=3DCprg>int i =3D this.j;<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>}<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>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(4,9): error CS0027: Keyword this is not =
available in the=20
current context<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">This variable is =
available only=20
in certain places within a class type and can be assumed as the first =
parameter=20
to the functions in the type. If we have a function abc accepting two =
ints as a=20
parameter, we would write it as abc(int i, int j). In class yyy, the =
function=20
gets rewritten as abc( yyy this, int i, int j). <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">This is passed as the =
first=20
parameter and refers to the class the function resides in. As the =
programming=20
language C++ called this reference variable as this, C# copied the same =
concept=20
but with a small change for structures.<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">Base<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">We use the variable =
base in a=20
completely different manner. A function in the base class gets hidden by =
the=20
same function name in a derived class.<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 a =3D new aaa();<o:p></o:p></P>
<P class=3DCprg>a.abc();<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 virtual void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy abc");<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 xxx : yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public override void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("xxx abc");<o:p></o:p></P>
<P class=3DCprg>base.abc();<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 : xxx<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public override void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("aaa abc");<o:p></o:p></P>
<P class=3DCprg>base.abc();<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>aaa abc<o:p></o:p></P>
<P class=3DCoutput>xxx abc<o:p></o:p></P>
<P class=3DCoutput>yyy abc<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">The base variable has =
absolutely=20
nothing to do with the modifiers new and override. It simply calls the =
function=20
in the base class without bothering about new or override to its =
existing=20
function. It simply does not care at all. Thus, using base, we can call =
a=20
function from the base class irrespective of the modifiers on it.=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">We will get an error =
if base is=20
used in an abstract function. Base, similar to this, is valid only in a=20
constructor, instance method or accessor. All the methods from the =
viewpoint of=20
base, are non virtual. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Writing base.abc will =
caste the=20
this pointer of the current class to its lower class, i.e. the base =
class.=20
Internally base.abc in class aaa derived from class xxx becomes =
((xxx)this).abc=20
and ditto for an indexer access. <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">Thus, base and this =
are=20
conceptually similar, except that one acts on a base class, the other on =
a=20
derived class.<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>xxx a =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>a.abc();<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 virtual void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy abc");<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 xxx : yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public override void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("xxx abc");<o:p></o:p></P>
<P class=3DCprg>((yyy)this).abc();<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">Do not run the above =
program as=20
it will take you to a trip to the moon. Just does not stop at all. All =
that we=20
said earlier was only conceptually true. You cannot replace a base with =
a this.=20
The programming language contains both of them and cannot be used=20
interchangeably. <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"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">A struct variable has =
a free=20
default constructor always available. Thus unlike a class =
initialization, which=20
explicitly initializes the instance variables to zero, the constructor =
in the=20
case of the struct does the same.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>

⌨️ 快捷键说明

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