is和as之间的区别

时间:2010-09-24 10:59:49

标签: c# casting

请说明C#

isas关键字之间的区别

15 个答案:

答案 0 :(得分:139)

is operator检查对象是否可以转换为特定类型。

示例:

if (someObject is StringBuilder) ...

作为

as operator尝试将对象强制转换为特定类型,如果失败则返回null。

示例:

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

也相关:

铸造

cast operator尝试将对象强制转换为特定类型,并在失败时抛出异常。

示例:

StringBuilder b = (StringBuilder)someObject.

答案 1 :(得分:29)

IS As 之间的区别是..

IS - 运算符是否用于检查具有给定类型的对象的兼容性,并将结果返回为布尔值(True或False)。

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

实施例。

Student s = obj as Student;

相当于:

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

答案 2 :(得分:6)

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

if(obj is string)
{
     ...
}

请注意,在这种情况下,您必须使用额外的显式强制转换来将obj作为字符串。

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

string str = obj as string;
if(str != null)
{
     ...
}

答案 3 :(得分:6)

两个'是'和'as'关键字用于C#中的类型转换。

当您查看两个关键字的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
}

以上C#代码的IL代码位于附图中  enter image description here

关键字用法的IL代码包含 isinsta castclass 的IL指令。 但作为关键字使用的IL代码只有 isinsta

在上面提到的用法中,将使用两次类型转换,其中使用关键字,并且只使用关键字一次。

注意:如果您使用'is'关键字检查某些条件,对类型结果没有任何兴趣,那么此处也只会出现类型种姓。 即。

if (baseclassInstance is DerivedClass)
{
   // do something based on the condition check.
}

'是'和'as'将根据需要使用关键字。

答案 4 :(得分:2)

我会说:在线阅读MSDN,但现在是:

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

as运算符永远不会抛出异常。

答案 5 :(得分:2)

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

作为演员。有了它,我们可以获得性能并避免在演员表无效时出现异常。无法进行演员表时返回Null。对于参考类型,建议使用as-cast。它既快又安全。我们可以测试结果变量对null,然后使用它。这消除了额外的演员

答案 6 :(得分:2)

  1. 是运算符检查对象是否与给定的内容兼容 输入基于true或false的结果。
  2. 用于将一种类型转换为另一种类型和转换 失败结果为null,除非然后引发异常。 通过示例https://blogs.msdn.microsoft.com/prakasht/2013/04/23/difference-between-direct-casting-is-and-as-operator-in-c/
  3. 查看链接以便更好地理解

答案 7 :(得分:1)

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

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

答案 8 :(得分:1)

is:is运算符用于检查对象的运行时类型是否与给定类型兼容

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

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

答案 9 :(得分:1)

查看下面的YouTube视频,以更具说明性和视觉性的方式解释差异: -

https://www.youtube.com/watch?v=IKmRtJcRX_I

下面是代码解释的长答案。

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

object ocust = new Customer();

if (ocust is Customer)
{ 

“AS”关键字有助于从一种类型转换为其他类型。例如,在下面的代码中,我们将对象转换为字符串数据类型。如果“AS”关键字无法键入强制转换,则返回NULL。

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

答案 10 :(得分:1)

两个操作员都用于安全型铸造。

AS运营商:

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

IS运营商:

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

答案 11 :(得分:1)

MyClass myObject = (MyClass) obj;

VS

MyClass myObject = obj as MyClass;

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

只会返回true或false

答案 12 :(得分:1)

IS和AS都用于安全类型转换

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.`

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`

答案 13 :(得分:1)

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

if(obj is AnimalObject)
{
 //Then Work
}

以操作员身份

as运算符执行与is运算符相同的工作,但区别在于它不是bool,如果它们与该类型兼容,则返回对象,否则返回null。换句话说,'as'运算符用于执行兼容类型之间的转换。

例如

Type obj = Object as Type;

截至目前的优势 对于is运算符,要进行强制类型转换,我们需要执行两个步骤:

Check the Type using is
If it’s true then Type cast

实际上,这会影响性能,因为每次CLR都要遍历继承层次结构,并根据指定的类型检查每个基本类型。

为避免这种情况,请使用as,它将一步完成。仅在检查类型时,才应使用is运算符。

答案 14 :(得分:-3)

"是"用于类型之间的相等性检查并返回bool值,而" as"用于类型转换,如果转换无效而不是异常,则返回null