Env .: C# 6, Visual Studio 2015 CTP 6Statische Konstruktor arbeitet nicht für structs
Gegeben das folgende Beispiel:
namespace StaticCTOR
{
struct SavingsAccount
{
// static members
public static double currInterestRate = 0.04;
static SavingsAccount()
{
currInterestRate = 0.06;
Console.WriteLine("static ctor of SavingsAccount");
}
//
public double Balance;
}
class Program
{
static void Main(string[] args)
{
SavingsAccount s1 = new SavingsAccount();
s1.Balance = 10000;
Console.WriteLine("The balance of my account is \{s1.Balance}");
Console.ReadKey();
}
}
}
Die statische Ctor ist nicht für die ausgeführten irgendein Grund. Wenn ich SavingsAccount als eine Klasse anstelle einer Struktur deklariere, funktioniert es gut.
Schauen Sie sich [diesen Link] (http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx) an, um parameterlose Konstruktoren einzurichten in Strukturen. –