fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. new Child1();
  8. new Child2();
  9. }
  10. }
  11.  
  12. class Parent
  13. {
  14. public Parent() { DoSomething(); }
  15. protected virtual void DoSomething() {}
  16. }
  17.  
  18. class Child1 : Parent
  19. {
  20. private string foo = "FOO";
  21. protected override void DoSomething() => Console.WriteLine(foo.ToLower());
  22. }
  23.  
  24. class Child2 : Parent
  25. {
  26. private string foo;
  27. public Child2() { foo = "FOO"; }
  28. protected override void DoSomething() => Console.WriteLine(foo.ToLower());
  29. }
Runtime error #stdin #stdout #stderr 0.01s 134976KB
stdin
Standard input is empty
stdout
foo
stderr
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at Child2.DoSomething () [0x00000] in <302b0a83289e4df688d6c9a0429c0c48>:0 
  at Parent..ctor () [0x00006] in <302b0a83289e4df688d6c9a0429c0c48>:0 
  at Child2..ctor () [0x00000] in <302b0a83289e4df688d6c9a0429c0c48>:0 
  at Test.Main () [0x00006] in <302b0a83289e4df688d6c9a0429c0c48>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at Child2.DoSomething () [0x00000] in <302b0a83289e4df688d6c9a0429c0c48>:0 
  at Parent..ctor () [0x00006] in <302b0a83289e4df688d6c9a0429c0c48>:0 
  at Child2..ctor () [0x00000] in <302b0a83289e4df688d6c9a0429c0c48>:0 
  at Test.Main () [0x00006] in <302b0a83289e4df688d6c9a0429c0c48>:0