Fork me on GitHub

1/12/2010

[C#] the error "Object reference not set to an instance of an object"

會發生這個 error 的原因是: The code is trying to access a member of a reference type variable that is set to null.

當使用automatic properties 的時候,如果沒有初始化 value type 的 property 會是 0, reference type 則會是 null。因此在建構子當中要針對 properties 做初始化的動作 ( "new" )

參考: http://blogs.msdn.com/csharpfaq/archive/2004/05/06/127647.aspx

...

No comments:

Post a Comment