I'm getting a System.TypeInitializationException exception on line 9 when running this code in which I attempt to fill a generic list in the class'static constuctor.
using System;
using System.Collections.Generic;
namespace ConsoleApplication5_static_constructor {
public static class DataRepository {
public static List<DefinedField> Tables;
static DataRepository() {
Console.WriteLine("static DataRepository constructor fired");
Tables.Add(new DefinedField("ID")); **//this is line 9**
}
}
public class DefinedField {
string _tableName;
public DefinedField(string tableName) {
_tableName = tableName;
}
public string TableName {
get { return _tableName; }
set { _tableName = value; }
}
}
}
Call code:
using System.Collections.Generic;
namespace ConsoleApplication5_static_constructor {
class Program {
static void Main(string[] args) {
List<DefinedField> x = DataRepository.Tables;
}
}
}
What exactly is causing the error and how do i solve it, please?
Edit: Also there is an inner exception of the type NullReferenceException Is a static constructor not capable of initializing new objects?