c#未调用构造函数

c#未调用构造函数,c#,constructor,xamarin-studio,C#,Constructor,Xamarin Studio,我的代码只是打印: welcome to battleship 但是船的坐标或船的创建 甚至最后“按任意键退出” 没有打印 我怀疑构造函数,看起来它没有被调用,但为什么我的程序在计数而没有崩溃呢 我用Xamarin Studio在Mac上运行代码 代码: 使用系统; 使用System.Collections.Generic; 名称空间战舰 { 类主类 { 公共静态void Main(字符串[]args) { 控制台.WriteLine(“欢迎来到战舰”); EnumShipClassifica

我的代码只是打印:

welcome to battleship
但是船的坐标或船的创建 甚至最后“按任意键退出” 没有打印

我怀疑构造函数,看起来它没有被调用,但为什么我的程序在计数而没有崩溃呢

我用Xamarin Studio在Mac上运行代码

代码:

使用系统;
使用System.Collections.Generic;
名称空间战舰
{
类主类
{
公共静态void Main(字符串[]args)
{
控制台.WriteLine(“欢迎来到战舰”);
EnumShipClassification.ShipClassification cruiser=EnumShipClassification.ShipClassification.theCruiser;
坐标起点坐标=新坐标(5,5);
EnumDirection.Direction方向=EnumDirection.Direction.west;
船舶=新船舶(巡洋舰、星际坐标、方向);
Console.WriteLine(“ship created”);/@debug
控制台写入线(“所有坐标:+ship.coordinates”);
//@结束
控制台。WriteLine(“按任意键退出”);
Console.ReadLine();
}
}
公船
{
public int size;//来自wikipedia的值
public int amount;//来自wikipedia的值
公共列表坐标=新列表();
公共船舶分类。船舶分类;
公共船舶(EnumShipClassification.ShipClassification分类,坐标起点坐标,EnumDirection.Direction-toDirection)
{
Console.WriteLine(“调用了Ship构造函数”);//@debug
这个。分类=分类;
Console.WriteLine(“添加了分类”);/@debug
开关(分类)
{
案例枚举ShipClassification.ShipClassification.theBattleship:size=5;amount=1;break;
案例EnumShipClassification.ShipClassification.theCruiser:size=4;amount=2;break;
案例EnumShipClassification.ShipClassification.theDestroyer:size=3;amount=3;break;
案例EnumShipClassification.ShipClassification.theSubmarine:大小=2;金额=4;中断;
默认:中断;
}
WriteLine(“开关以大小{0}和数量{1}结束”,大小,数量);
对于(int i=0;i
更改

public Coordinate invalid = new Coordinate(-1, -1);

公共坐标无效=新坐标(-1,-1);
导致堆栈溢出。这是因为新坐标在坐标类内初始化,所以每次必须创建新坐标时都要初始化。将字段设为静态,以便创建一次并可用于所有实例

如果引用不应更改,也可以添加
readyonly
关键字

public readonly static Coordinate invalid = new Coordinate(-1, -1);

您已经在这里发布了很多代码,但对于
public static Coordinate invalid = new Coordinate(-1, -1);
public readonly static Coordinate invalid = new Coordinate(-1, -1);