为什么这个程序仍然输出“not null”?

时间:2013-01-10 22:25:09

标签: c# .net

为什么即使一切都是空的,这个程序仍会输出“not null”?!我必须改变什么才能使它成为空?

“您的帖子没有太多的背景来解释代码部分;请更清楚地解释您的情况。”对不起......

public interface IDrawable 
{
    void Draw();
}

public interface IAdvancedDraw : IDrawable
{
    void DrawInBoundingBox();
    void DrawUpsideDown();
}

public class BitmapImage : IAdvancedDraw
{
    public void Draw() { }
    public void DrawInBoundingBox() { }
    public void DrawUpsideDown() { }
}

class Program
{

    static void Main( string[] args )
    {

        BitmapImage myBitmap = new BitmapImage();

        if ((IAdvancedDraw)myBitmap != null){
            Console.WriteLine("not null"); 
        }

        Console.ReadLine();
    }
}

2 个答案:

答案 0 :(得分:4)

因为它已初始化,所以它不为空。

BitmapImage myBitmap = new BitmapImage();

new Operator

答案 1 :(得分:1)

你总是得到"not null',因为myBitmap中总是 - 毕竟,你刚刚创建了一个new BitmapImage()并把它放在那里!