C# is和as关键字之间的区别

C# is和as关键字之间的区别,c#,casting,C#,Casting,请告诉我是和作为关键字在C#中的区别,我想说:在线阅读MSDN,但这里是: is运算符检查对象是否与给定类型兼容,计算结果为布尔值:true或false as运算符永远不会引发异常。是 `student stud = new student(){} // let say boys as derived class boys boy = stud as boys;//this returns null since we cant convert stud type from base class

请告诉我
作为
关键字在C#

中的区别,我想说:在线阅读MSDN,但这里是:

is运算符检查对象是否与给定类型兼容,计算结果为布尔值:true或false

as运算符永远不会引发异常。

`student stud = new student(){}
 // let say boys as derived class
boys boy = stud as boys;//this returns null since we cant convert stud type from base class to derived class
student stud = new boys()
boys boy = stud as boys;// this returns not null since the obj is pointing to derived class`
检查对象是否可以强制转换为特定类型

例如:

if (someObject is StringBuilder) ...
StringBuilder b = someObject as StringBuilder;
if (b != null) ...
StringBuilder b = (StringBuilder)someObject.
作为 尝试将对象强制转换为特定类型,如果失败,则返回null

例如:

if (someObject is StringBuilder) ...
StringBuilder b = someObject as StringBuilder;
if (b != null) ...
StringBuilder b = (StringBuilder)someObject.

还涉及:

铸造 尝试将对象强制转换为特定类型,如果失败,则抛出一个exeption

例如:

if (someObject is StringBuilder) ...
StringBuilder b = someObject as StringBuilder;
if (b != null) ...
StringBuilder b = (StringBuilder)someObject.

As运算符类似于强制转换,但如果失败,则返回null而不是异常


Is运算符用于检查一个对象是否与某个类型兼容。它通常用于If语句。

is关键字检查其左侧的值是否是右侧类型的实例。例如:

if(obj is string)
{
     ...
}
string str = obj as string;
if(str != null)
{
     ...
}
请注意,在这种情况下,您必须使用额外的显式转换才能将obj转换为字符串

as关键字用于强制转换可为null的类型。如果指定的值不是指定类型的实例,则返回null。例如:

if(obj is string)
{
     ...
}
string str = obj as string;
if(str != null)
{
     ...
}
:is运算符用于检查对象的运行时类型是否与给定类型兼容

:as运算符用于在兼容类型之间执行转换

object s = "this is a test";
string str=string.Empty;
if( s is string)
    str = s as string;

As的区别在于

IS-IS运算符用于检查对象与给定类型的兼容性,并以布尔值形式返回结果(True或False)

AS-AS运算符用于将对象强制转换为给定类型或类

相当于:

Student s = obj is Student ? (Student)obj : (Student)null;

请看下面的youtube视频,该视频以更直观的方式解释了这种差异:-

下面是详细的答案和代码解释

“IS”关键字用于检查对象是否与类型兼容。例如,在下面的代码中,我们检查“ocust”对象是否是“Customer”类的类型

object ocust = new Customer();

if (ocust is Customer)
{ 
“AS”关键字有助于从一种类型转换为另一种类型。例如,在下面的代码中,我们将对象转换为字符串数据类型。如果“AS”关键字无法键入cast,则返回NULL

object o = "somestring";
string str = o as string;

两个操作器均用于安全型铸造

作为操作员:

AS运算符还检查给定对象的类型是否与新对象类型兼容。此关键字将检查给定对象的类型是否与新对象类型兼容。如果它与新的不兼容,那么它将返回NULL

IS操作员:

BaseClass baseclassInstance = new DerivedClass();
DerivedClass derivedclassInstance;

if (baseclassInstance is DerivedClass)
{
   derivedclassInstance = (DerivedClass)baseclassInstance;
   // do something on derivedclassInstance
}


derivedclassInstance = baseclassInstance as DerivedClass;

if (derivedclassInstance != null)
{
   // do something on derivedclassInstance
}

此运算符检查对象的类型是否与新对象兼容。如果它兼容,则返回true,否则返回false。

是运算符,一个强制转换,如果成功,则返回true。如果强制转换失败,则返回false。使用它,您无法捕获转换后的变量。当在if语句和表达式中检查类型时,此运算符最有用。只有在不再需要结果变量以供进一步使用时,is强制转换才是理想的

就像演员一样。有了它,我们可以获得性能,并在强制转换无效时避免异常。当无法进行强制转换时,返回Null。对于参考类型,建议使用铸态。它既快速又安全。我们可以针对null测试结果变量,然后使用它。这消除了额外的强制转换

  • is运算符检查对象是否与给定的 根据true或false键入结果
  • as用于将一个类型转换为另一个类型并在转换时使用 失败结果为空,但引发异常除外。 请参阅链接,以便更好地理解示例
  • vs

    如果obj不是MyClass,则第二个将返回null,而不是抛出类强制转换异常

    `student stud = new student(){}
     // let say boys as derived class
    boys boy = stud as boys;//this returns null since we cant convert stud type from base class to derived class
    student stud = new boys()
    boys boy = stud as boys;// this returns not null since the obj is pointing to derived class`
    

    is将只返回true或false

    由于关键字用于C#中的类型转换,因此
    is
    都将返回true或false

    `student stud = new student(){}
    if(stud is student){} // It returns true // let say boys as derived class
    if(stud is boys){}// It returns false since stud is not boys type
     //this returns true when,
    student stud = new boys() // this return true for both if conditions.`
    
    当你看一下这两个关键词的IL代码时,你会很容易地发现它们的区别

    C#代码:

    BaseClass baseclassInstance = new DerivedClass();
    DerivedClass derivedclassInstance;
    
    if (baseclassInstance is DerivedClass)
    {
       derivedclassInstance = (DerivedClass)baseclassInstance;
       // do something on derivedclassInstance
    }
    
    
    derivedclassInstance = baseclassInstance as DerivedClass;
    
    if (derivedclassInstance != null)
    {
       // do something on derivedclassInstance
    }
    
    IL代码(以上C代码见附图):

    的IL代码是
    关键字用法,包含
    isinsta
    castclass
    的IL指令
    但是
    as
    关键字用法的IL代码只有
    isinsta

    在上述用法中,使用
    is
    关键字时将发生两次类型转换,使用
    as
    关键字时仅发生一次类型转换

    注意:如果您正在使用
    is
    关键字检查某些条件,并且对typecast结果没有任何兴趣,那么只有一个typecast,即

    if (baseclassInstance is DerivedClass)
    {
       // do something based on the condition check.
    }
    
    is
    as
    关键字将根据需要使用。

    is和as均用于安全型铸造 是关键字--> 检查给定对象的类型是否与新对象类型兼容。它从不抛出异常。这是布尔类型..返回true或false

    `student stud = new student(){}
    if(stud is student){} // It returns true // let say boys as derived class
    if(stud is boys){}// It returns false since stud is not boys type
     //this returns true when,
    student stud = new boys() // this return true for both if conditions.`
    
    AS关键字: 检查给定对象的类型是否与新对象类型兼容。如果给定对象与新对象兼容,则返回非null,否则返回null。。这会引发异常

    `student stud = new student(){}
     // let say boys as derived class
    boys boy = stud as boys;//this returns null since we cant convert stud type from base class to derived class
    student stud = new boys()
    boys boy = stud as boys;// this returns not null since the obj is pointing to derived class`
    

    是操作员 C#中的is运算符用于检查对象类型,并返回bool值:如果对象类型相同,则返回true;否则返回false。 或者“is”操作符用于检查对象的运行时类型是否与给定类型兼容。 对于空对象,它返回false e、 g

    作为操作员

    as运算符执行与is运算符相同的任务,但其差异不是bool,而是返回对象(如果它们是c)