title: Access Violation Exception during access of field of Object
body: <p>I'm encountering a realy strange behaviour when using Linq in my .Net Core 2.0, ASP.Net react project:</p>
  
  <p>When accessing a field of an object which gets instantiated inside an Linq "Select" selector, it throws an "Access Violation Exception"</p>
  
  <p>Steps to reproduce (hopefully):</p>
  
  <ul>
  <li>Visual Studio 2017, 15.7.4</li>
  <li>Create new ASP.Net Core 2.0 Project</li>
  <li>Choose "React" template</li>
  <li><p>alter the <strong>SampleDataController.cs</strong> as follows:</p>
  
  <pre><code>[HttpGet("[action]")]
  
  public string Objects()
  {
      var rng = new Random();
      var ret = Enumerable.Range(1, 5).Select(index =&gt; 
          new TableDef("User")
              .AddField(new StringField("ID", Guid.NewGuid().ToString()))
              .AddField(new StringField("Name", "User " + index.ToString()))
      );
  
      return "[" +
              ret.Select(x =&gt; x.ToJSONString()).Aggregate((current, next) =&gt; current + ", " + next) +
          "]";
  }
  </code></pre></li>
  <li><p>With the following Classes:</p>
  
  <pre><code>public class TableDef
  {
      public string ID { get; private set; } = Guid.NewGuid().ToString();
      public string LockedBy { get; private set; } = "";
      public string Name { get; private set; }
      public FieldDef[] Fields { get; private set; }
  
      public TableDef(string name)
      {
          Name = name;
          Fields = new FieldDef[0];
      }
  
      public TableDef AddField(FieldDef field)
      {
          field.Parent = this;
          var flds = this.Fields;
          Array.Resize(ref flds, flds.Length + 1);
          flds[flds.Length - 1] = field;
          this.Fields = flds;
          return this;
      }
  
      public string ToJSONString()
      {
          return @" { ""TableDef"": {"" 
              ""DefID"": """ + ID + @""", 
              ""DefName"": """ + Name + @""", 
              ""DefLockedBy"": """ + LockedBy + @"""}, " + 
                  Fields
                      .Select(x =&gt; ('"' + x.Name + @""" : """ + x.Value + '"'))
                      .Aggregate((current, next) =&gt; current + ", " + next) +
              "}";
      }
  }
  
  
  
  public abstract class FieldDef 
  {
      public string ID { get; protected set; } = Guid.NewGuid().ToString();
      public string Name { get; protected set; }
      public abstract string Value { get; set; }        
      public abstract string DefaultValue { get; set;  }
      public Boolean ReadOnly { get; protected set; } = false;
      public Boolean IsDirty { get; set; } = false;
      public TableDef Parent {
          get { return Parent; }
          set {
              this.Parent = value;
              ID = Parent.Name.Replace(" ", "") + "_" + Name.Replace(" ", "");
          }
      }
  }
  
  
  public class StringField : FieldDef
  {
      public override string Value {
          get { return (IsDirty) ? Value : DefaultValue; }
          set { this.Value = value; }
      }
      public override string DefaultValue { get; set; }
  
      public StringField(string name, string defaultValue)
      {
          Name = name;
          DefaultValue = defaultValue;
      }
  
      public StringField(string name, string defaultValue, Boolean readOnly)
      {
          Name = name;
          DefaultValue = defaultValue;
          ReadOnly = readOnly;
      }
  }
  </code></pre></li>
  </ul>
  
  <p>When running the code and clicking on the "fetch data" link, the application dies (at least in my case) completely without even an error in Visual studio. The debugging just ends, leaving me with the following debug output:</p>
  
  <pre><code>The program '[20584] dotnet.exe: Program Trace' has exited with code 0 (0x0).
  The program '[20584] dotnet.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
  The program '[20584] dotnet.exe: Managed (CoreCLR)' has exited with code -1 (0xffffffff).
  </code></pre>
  
  <p>When commenting the line <code>field.Parent = this;</code> in <code>public TableDef AddField(FieldDef field)</code> the code runs smoothly. What may be worth noting is, that the program dies, even though I put a Breakpoint on this line. As this takes 2 or 3 seconds, I managed to read some variables from the field in the "Auto" intermediate window in the debugger, and they look perfectly fine. As soon as i try to access the parent field, the application dies though.</p>
  
  <p>I'm pretty sure it's some inheritance stuff falsly interpreted by myself.</p>
  
  <p>Any help is much appreciated. Thanks.</p>