欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断数组、集合list、string、int、double等是否为空,判断是否为值类型

程序员文章站 2024-02-21 10:45:58
...

1.判断数组、集合list、string、int、double等是否为空

private void button35_Click(object sender, EventArgs e)
        { 
            string aa = "";
            string bb = null;
            string cc = string.Empty;
            qq dd = new qq();
            string[] ee = new string[1] { "1"};
            List<string> ff = new List<string>();
            //Console.WriteLine(GetIsNull(aa));
            //Console.WriteLine(GetIsNull(bb));
            //Console.WriteLine(GetIsNull(cc));
            //Console.WriteLine(GetIsNull(dd));

            Console.WriteLine(GetIsNull(ee));

            //Console.WriteLine(GetIsNull(ff));
        }



public bool GetIsNull(object obj)
        {
            if (obj is Array)
            {
                return ((object[])obj).Length > 0 ? true : false;
            }
            else
            if (obj is IList)//using System.Collections.Generic;
            {
                return ((IList)obj).Count > 0 ? true : false;
            }
            else
            {
                string aa = Convert.ToString(obj);
                if (!string.IsNullOrEmpty(aa)&&aa!=null)
                {
                    return false;
                }
            } 
            return false;
        }

 

2.。是否为值类型

object  val

1. if (val.GetType ()==typeof (int))
{
  ........
}

2.  if (val is int)
             {
             ............

    }
object aa="123";
var bb= aa.GetType();//获取类型
//这里bb就为string,注意:如果object为null  GetType()会抛出异常