This articleArticleI want to summarize my understanding of the IS and as operators to review the basics of C.
Is OPERATOR:Check whether the object is compatible with the specified type.
Note:
1>: If the provided expression is not empty and the provided object can be forcibly converted to the provided type without causing an exception, the calculation result of the is expression will be true, otherwise, false is returned.
1): The expression is null. False is returned.
// The expression is empty.
Object Oo = Null ;
Bool Isstudent3 = Oo Is Student;
2): The expression content is not empty, but an exception occurs during forced conversion. False is returned.
Oo = New Object ();
Bool Isstudent4 = Oo Is Student;
3): If the expression is null, no exception is thrown because no correct object is used for type verification.
2>: The is operator only considers reference conversion, boxing conversion, and unboxing conversion. The followingProgramCompile-time error (CTE): The expression is always true or false.
Int I = 5 ;
If (I Is Decimal )
{
// Tip: The given expression is never of the ("decimal") type.
}
If (I Is Int )
{
// The given expression is always of the ("int") type.
}
3>: The is operator cannot be overloaded.
4>: The first operand of the "is" or "as" operator cannot be a Lambda or anonymous expression.
If (( Delegate ( Int I ){ Return I ;}) Is Testdelegate)
{
// Tip: The first operand of the "is" or "as" operator cannot be a Lambda or anonymous expression.
}
If (X) => { Return X ;}) Is Testdelegate)
{
// Tip: The first operand of the "is" or "as" operator cannot be a Lambda or anonymous expression.
}
As OPERATOR:Converts compatible reference types.
Note:
1>: The as operator is similar to forced conversion, but different. When the object is null, null is returned instead of throwing an exception.
Object _ Object = Null ;
Student _ s = _ Object As Student;
_ Object as student is actually equivalent to _ object is student? (Student) _ OBJECT: NULL; in this case, we can refer toCodeSee some conclusions: first, we instantiate a base class object.
Object O = New Student ();
Then execute:
1): O is student, corresponding to the Il code
. Method Private Hidebysig Static Void Main ( String [] ARGs) Pencil managed
{
. Entrypoint
// Code size 18 (0x12)
. Maxstack 2
. Locals Init ([ 0 ] Object O,
[ 1 ] Bool Isstudent2)
Il_0000: NOP
Il_0001: newobj instance Void Jmtest. consoleapplication1.student:. ctor ()
Il_0006: stloc. 0
Il_0007: ldloc. 0
Il_0008: isinst jmtest. consoleapplication1.student
Il_000d: ldnull
Il_000e: CGT. Un
Il_0010: stloc. 1
Il_0011: Ret
} // End of method program: Main
2): O as student, corresponding to the Il code
. Method Private Hidebysig Static Void Main ( String [] ARGs) Pencil managed
{
. Entrypoint
// Code size 15 (0xf)
. Maxstack 1
. Locals Init ([ 0 ] Object O,
[ 1 ] Class Jmtest. leleapplication1.student S2)
Il_0000: NOP
Il_0001: newobj instance Void Jmtest. consoleapplication1.student:. ctor ()
Il_0006: stloc. 0
Il_0007: ldloc. 0
Il_0008: isinst jmtest. consoleapplication1.student
Il_000d: stloc. 1
Il_000e: Ret
} // End of method program: Main
Conclusion: isinst is executed for both segments of IL code. It indicates whether the reference of the test object is an instance of a specific class, And stloc.1 after the as operator indicates: from the top of the computing stack, the list of local variables from the current value to index 1 is displayed. The usage of these two IL statements can describe the partial relationship between the is and the as operator.
2>: The as operator only performs reference conversion and packing conversion, and cannot perform other conversions. The following code is incorrect:
Object _ Object = Null ;
// Attempting to convert an object into a delegate
Student _ s = _ Object As Student;
Testdelegate Test = _ S As Testdelegate;