是否有更多的C#" AS"关键字不是简单的铸造? [英] Is there more to the C# "as" keyword than simple casting?

查看:134
本文介绍了是否有更多的C#" AS"关键字不是简单的铸造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过约什 - 史密斯的CommandSink代码的工作显然不明白,一些关于为 。关键字在C#



我不明白为什么他写的一行:

 的IsValid = _fe!= NULL || _fce!= NULL; 



,因为他只需要这样写:

 的IsValid = depObj!= NULL; 

由于它永远不会是这样的_fe将是无效和_fce不为空,或者反之亦然,对?还是我失去了一些东西有关如何作为铸就变量?



 类CommonElement 
{
只读FrameworkElement的_fe;
只读FrameworkContentElement上_fce;

公共只读BOOL的IsValid;

公共CommonElement(DependencyObject的depObj)
{
_fe = depObj为FrameworkElement的;
_fce = depObj为FrameworkContentElement上;

的IsValid = _fe!= NULL || _fce!= NULL;
}
...



答:



答案是马克在他的评论中说,这是整点为 - 它的不会的抛出一个异常 - 它只是空报告



和这里是证明:

 使用系统; 

命名空间TestAs234
{
类节目
{
静态无效的主要(字串[] args)
{
客户的客户=新客户();
员工员工=新员工();

Person.Test(客户);
Person.Test(员工);
到Console.ReadLine();
}

}

类Person
{
公共静态无效测试(obj对象)
{
人人= OBJ的客户;

如果(人== NULL)
{
Console.WriteLine(人空);
}
,否则
{
Console.WriteLine(人型{0},obj.GetType());
}
}
}

类客户:消费
{
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
}

类员工:人
{
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
}

}


解决方案

将返回您请求的类型的对象,如果操作数是兼容的。如果不是,它将返回。如果你使用,这是可能的,中投会失败,你需要检查,以确保引用是有效的。



例如,如果depObj是类型字符串,它不会是,但它也不能够被转换到要么请求的类型和这两个变量的将成为


I'm working through Josh Smith's CommandSink code obviously do not understand something about the "as" keyword in C#.

I don't understand why he wrote the line:

IsValid = _fe != null || _fce != null;

since he only needed to write:

IsValid = depObj != null;

Since it would never be the case the _fe would be null and _fce not null, or visa versa, right? Or am I missing something about how "as" casts variables?

class CommonElement
{
    readonly FrameworkElement _fe;
    readonly FrameworkContentElement _fce;

    public readonly bool IsValid;

    public CommonElement(DependencyObject depObj)
    {
        _fe = depObj as FrameworkElement;
        _fce = depObj as FrameworkContentElement;

        IsValid = _fe != null || _fce != null;
    }
    ...

ANSWER:

The answer is what Marc said in his comment "that is the whole point of "as" - it won't throw an exception - it just reports null."

and here is the proof:

using System;

namespace TestAs234
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Employee employee = new Employee();

            Person.Test(customer);
            Person.Test(employee);
            Console.ReadLine();
        }

    }

    class Person
    {
        public static void Test(object obj)
        {
            Person person = obj as Customer;

            if (person == null)
            {
                Console.WriteLine("person is null");
            }
            else
            {
                Console.WriteLine("person is of type {0}", obj.GetType());
            }
        }
    }

    class Customer : Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class Employee : Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

}

解决方案

as will return an object of the type you requested, if the operand is compatible. If it isn't, it will return null. If you use as and it is possible that the cast will fail, you need to check to make sure the reference is valid.

For example, if depObj was of type String, it would not be null, but it would also not be able to be converted to either of the requested types and both of those variables would become null.

这篇关于是否有更多的C#" AS"关键字不是简单的铸造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆