类型为system . formatexception的未处理异常;输入字符串格式不正确
本文关键字:输入 字符串 格式 不正确 异常 未处理 system formatexception 类型 | 更新日期: 2023-09-27 18:12:39
我目前正在学习c#,我正试图让我的头围绕类继承,并写了一些代码来显示一行到控制台,但我在这个帖子主题中遇到了错误问题。
在字符串的子类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}"...