声明一个对象c#

时间:2011-01-27 14:24:00

标签: c# .net-2.0

我有以下代码:

object myObject = new Object()

我试图在分配空值时检查myObject的值。

所以我用

if (myObject !=null)

....但这似乎不起作用,因为当myObject = null

的值时它仍然在if语句中执行代码
        if (lawyerResponsibleUserField != null
                && lawyerResponsibleUserField.Id != null)
            {
                 // this effectively assigns Convert.ToString(null)
                objLawyerResponsibileValue = Convert.ToString(workflowProperties != null 
                    ? workflowProperties.Item[lawyerResponsibleUserField.Id]
                        : null );
            }
        if (objLawyerResponsibileValue != null)
        {
            lawyerResponsibleFieldValue = (SPFieldUserValue)objLawyerResponsibileValue;
        }

3 个答案:

答案 0 :(得分:3)

范 -

此转让声明 -

object myObject = new Object();

...使它不为空。 =)如果你写的话,它只会是null:

object myObject = null;

这里的目标是什么?

答案 1 :(得分:1)

试试这个

    object myObject = null;

    if(myObject != null)
    {
     // your code
    }

答案 2 :(得分:1)

试试这些,

    static void Main(string[] args)
    {
        object myObject = new Object();
        if (myObject == null)
        {
            Console.WriteLine("My Object is null");
        }
        else
        {
            Console.WriteLine("My Object is NOT null");
        }
    }

My Object is NOT null

    static void Main(string[] args)
    {
        object myObject = null;
        if (myObject == null)
        {
            Console.WriteLine("myObject is null");
        }
        else
        {
            Console.WriteLine("My Object is NOT null");
        }
    }

My Object is null