c# - Why I am getting null exception when using an Interface -


this question has answer here:

public class blah {     public bool whatever { get; set; }     public string whatyoujustsaid { get; set; } }  public interface iblah {     blah blahvalues { get; set; } }  class class1:iblah {     public blah blahvalues { get; set; } } 

and example:

   class1 c1 = new class1();    c1.blahvalues.whatyoujustsaid = "nothing";     c1.blahvalues.whatever = false; 

so how should change code blahvalues doesn't null?

you have initialize blahvalues. using object initializer, can done below:

class1 c1 = new class1() {    blahvalues = new blah()  }; 

as create c1 object, blahvalues it's default value, null. hence, when try assign value whatyoujustsaid , whatever null reference exception.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -