DotNET describes the difference between the IS and AS operators in the interface

xiaoxiao2021-03-06  49

We may need to know whether the object supports the interface in actual work, so that the corresponding method can be called, and there are IS and AS operators in the DOTNET to meet our judgment. Let's compare the difference between IS and AS in DOTNET.

The IS operator is in the form of an expression IS type. The IS operator can be converted to "type" in "expression" (must be a reference type) without throwing an exception, and the result is true. Below we take a look at the usage of IS:

// Convert only when safe, assume that the Document class implements the iStORABLE interface, and does not implement the // iCompressible interface, and there is read (), write (), etc. in the interface iStorable.

Document Doc = New Document ("Test Document");

IF (Document Is ISTORABLE) // Is it legal in this test

{

IStorable IsDoc = (iStorable) Doc; // Forced conversion, because the Document class only implements the iStORABLE interface, so conversion legal

Isdoc.read ();

}

// The following conversion is illegal

Document Doc = New Document ("Test Document");

IF (Document ICOMPRESSIBLE // IS is legal

{

ICompressible isDoc = (iCompressible) Doc; // Forced conversion, because the Document class does not implement the ICompRessible interface, so the conversion is not legal

Isdoc.read ();

}

IF tells us that it is only forced to convert only the object is the correct interface type. This leads to poor efficiency, why? Let's take a look at the MSIL code below:

IL_0023: isinst iCompressible // Keyword isinst is the MSIL code of IS, whether the test can be converted

IL_0028: brfalse.s il _0039

IL_

002A

: Ldloc.0

IL_002B: Castclass iCompressible // Here you need to test, first test, and then convert

IL_0030: LDLOC.2

..................

So the two tests caused poor efficiency, then we will take a look at the AS operator and compare them.

The form of the AS operator is: Expression AS type

The AS operator combines the IS operator and the conversion operation, first test whether the conversion is legal (ie, if the IS test is converted to true), if it is converted. If the conversion is not legal (ie, if the IS test is converted to false), the AS operator returns null, let's take a look at the example:

Document Doc = New Document ("Test Document");

IStorable isdoc = doc as iStorable;

IF (isdoc! = Null)

Isdoc.read ();

Else

Console.writeline ("iStorable Not Support")

Plenary:

Document Doc = New Document ("Test Document");

ICompressible isdoc = doc as iCompressible;

IF (isdoc! = Null)

Isdoc.read (); else

Console.writeline ("iCompressible Not Support")

Let's take another MSIL code:

IL_0023: Isinst iCompRessible

IL_0028: STLOC.2

IL_

002A

: Ldloc.2

IL_002B: brfalse.s il _0034

IL_0030: LDLOC.2

In this way, let's see that the conversion is only once, the efficiency is high.

So this time we know how to use IS, what should I use: If the purpose of our test object is to determine if it belongs to the required type, and if it is, it is necessary to convert it immediately, in which case The AS operator is more efficient, but sometimes we just test do not want to transform immediately, it may not be converted at all, just when the object is implemented, it is better to add it to a list. This is a better IS operator. s Choice.

转载请注明原文地址:https://www.9cbs.com/read-79692.html

New Post(0)