![](/img/trans.png)
[英]Unhandled Exception: System.FormatException: Input string was not in a correct format
[英]Unhandled exception of type “System.FormatException” Input string was not in correct format
我目前正在学习C#,并且试图绕过类继承,并编写了一些代码来向控制台显示一行,但是我在此帖子主题中遇到了一个问题。
它在string.format行的子类PC中引发错误。 我假设这与我传递的类型有关,但是到目前为止我尝试过的所有方法都无效。 非常感谢任何输入,因为我想确保在继续进行下一轮培训之前我已完全理解这一点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassInheritance
{
class Program
{
static void Main(string[] args)
{
PC myPC = new PC();
myPC.casetype = "Black SuperCool";
myPC.contract = true;
myPC.dateadded = DateTime.Now;
myPC.id = 1;
myPC.type = "Small form factor";
printEquipmentDetails(myPC);
Console.ReadLine();
}
private static void printEquipmentDetails(Equipment Equipment)
{
Console.WriteLine("Details: {0}", Equipment.DisplayEquipment());
}
}
abstract class Equipment
{
public int id { get; set; }
public string type { get; set; }
public bool contract { get; set; }
public DateTime dateadded { get; set; }
public abstract string DisplayEquipment();
}
class PC : Equipment
{
public string casetype { get; set; }
public override string DisplayEquipment()
{
return String.Format("(0} - {1} - {2} - {3} - {4}", this.id, this.type, this.contract.ToString(), this.dateadded.ToShortDateString(), this.casetype);
}
}
class Laptop : Equipment
{
public string batterylife { get; set; }
public override string DisplayEquipment()
{
return String.Format("(0} - {1} - {2} - {3} - {4}", this.id, this.type, this.contract, this.dateadded, this.batterylife);
}
}
}
在return String.Format("
( 0} - {1} - {2} - {3} - {4}"...
行中将{
替换为(
。
所以
return String.Format("
( 0} - {1} - {2} - {3} - {4}"...
将会
return String.Format("
{ 0} - {1} - {2} - {3} - {4}"...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.