最基础的——类实例化和不实例化的区别
有一个类
public class test { public string name { get; set; } public int id { get; set; } public bool sex { get; set; } }
调用
static void Main() { test t = new test(); //test t = null; Console.WriteLine(t.id); Console.WriteLine(t.name == null ? 1 : 0); Console.WriteLine(t.sex); Console.ReadKey(); }

说明,类在实例化之后就已经不是null了,此时所有的字段都是默认值,int 0;string null ;bool false
如果类没有实例化,而是直接直接接受其类型的对象来进行赋值,然后再 调用相应的属性等,是可以的,但是有时候由于程序不严谨会导致 赋值的对象是一个null,然后接下来就会报错,因为 null 怎么会有属性呢?
虽然很基础,但是也是很容易出错的
浙公网安备 33010602011771号