在网上看到有据类型判断转换有 as, is等判断方法,哪位大侠能给个比较全面的方法,谢谢!

解决方案 »

  1.   

    int abc = 5;
                Console.WriteLine(abc.GetType());
                Console.WriteLine(abc.GetTypeCode());
                Console.WriteLine(typeof(int).IsInstanceOfType(abc));
                if (abc is int)
                {
                    Console.WriteLine("true");
                }
      

  2.   

    资料网上多的是,总的来说is判断某个对象是否是某个类型的实例,as类似强制转换,当转换失败,得到nullclass Person{}
    class Teacher:Person{public void Study(){}}
    class Student:Person{public void Teach(){}}List<Person> personList= new List<Person>();
    personList.Add(new Teacher());
    personList.Add(new Student());
    personList.Add(new Teacher());foreach(Person person in personList)
    {
       if(person is Teacher)
       {
         Teacher teacher = person as Teacher;
         teacher.Teach();
       }
       else  if(person is Student)
       {
         Student teacher = person as Student;
         Student.Study();
       }
    }
      

  3.   

    上面代码有误class Person{}
    class Teacher:Person{public void Study(){}}
    class Student:Person{public void Teach(){}}List<Person> personList= new List<Person>();
    personList.Add(new Teacher());
    personList.Add(new Student());
    personList.Add(new Teacher());foreach(Person person in personList)
    {
       if(person is Teacher)
       {
         Teacher teacher = person as Teacher;
         teacher.Teach();
       }
       else  if(person is Student)
       {
         Student student = person as Student;
         student.Study();
       }
    }
      

  4.   

    as 运算符用于在兼容的引用类型之间执行转换...注意,as 运算符只执行引用转换和装箱转换。as 运算符无法执行其他转换,如用户定义的转换,这类转换应使用强制转换表达式来执行...is 运算符检查对象是否与给定类型兼容...注意,is 运算符只考虑引用转换、装箱转换和取消装箱转换。不考虑其他转换,如用户定义的转换...这些在MSDN中都能找到...
      

  5.   

    --------------------------------------------------------------------------------is运算符  [  可以检查对象是否与特定的类型兼容  ]  [返回true 或者说 false]
       
              [兼容表示对象是该类型或者派生于该类型]--------------------------------------------------------------------------------as运算符  [执行引用类型的显式类型转换]          [如果要转换的类型与指定的类型兼容;就会成功]          [如果类型不兼容;就会返回值null]object o1= "some string";
    object o2= 5
    string s1= o1 as string; //s1= "some string"
    string s2= o2 as string; //s2=null;
      

  6.   

    简单的说  iS是属于判断 数据类型的  如if(a is string)
             as是类似于强制类型转换的 所不同的是 如果出错的话 返回值是null 而不会发生异常
      

  7.   

    GetType()方法获取类型
    类型转换包括显示转换和隐式转换is/as操作符,是C#中用于类型转换的,提供了对类型兼容性的判断,从而使得类型转换控制在安全的范畴,提供了灵活的类型转换控制。
    is:检查对象类型的兼容性,并返回结果,true或者false; 
    不会抛出异常; 
    如果对象为null,则返回值永远为false。 
    as:
    检查对象类型的兼容性,并返回结果,如果不兼容就返回null; 
    不会抛出异常; 
    如果结果判断为空,则强制执行类型转换将抛出NullReferenceException异常。
      

  8.   

    cast(xx as int), is  是SQL 的  is null or nothing 是vb.net的convert.toINT32intprase
      

  9.   

    int a = Convert.ToInt32(xx);
      

  10.   


    // cs_keyword_as.cs
    // The as operator.
    using System;
    class Class1
    {
    }class Class2
    {
    }class MainClass
    {
        static void Main()
        {
            object[] objArray = new object[6];
            objArray[0] = new Class1();
            objArray[1] = new Class2();
            objArray[2] = "hello";
            objArray[3] = 123;
            objArray[4] = 123.4;
            objArray[5] = null;        for (int i = 0; i < objArray.Length; ++i)
            {
                string s = objArray[i] as string;
                Console.Write("{0}:", i);
                if (s != null)
                {
                    Console.WriteLine("'" + s + "'");
                }
                else
                {
                    Console.WriteLine("not a string");
                }
            }
        }
    }
    // cs_keyword_is.cs
    // The is operator.
    using System;
    class Class1
    {
    }
    class Class2
    {
    }class IsTest
    {
        static void Test(object o)
        {
            Class1 a;
            Class2 b;        if (o is Class1)
            {
                Console.WriteLine("o is Class1");
                a = (Class1)o;
                // Do something with "a."
            }
            else if (o is Class2)
            {
                Console.WriteLine("o is Class2");
                b = (Class2)o;
                // Do something with "b."
            }
            else
            {
                Console.WriteLine("o is neither Class1 nor Class2.");
            }
        }
        static void Main()
        {
            Class1 c1 = new Class1();
            Class2 c2 = new Class2();
            Test(c1);
            Test(c2);
            Test("a string");
        }
    }