gpt4 book ai didi

c# - C#运行时错误声明主体,因为它没有被标记为抽象,外部或局部

转载 作者:行者123 更新时间:2023-12-03 09:02:33 28 4
gpt4 key购买 nike

嗨,我在运行这段代码时遇到了一些麻烦,因为我收到Kennel.Kennel()的运行时错误,必须声明一个主体,因为它没有被标记为抽象,外部或部分。请提供建议和帮助。非常感激。

using System;
using System.Collections.Generic;
using System.Text;

public class Kennel : IComparable<Kennel>
{
private string _name;
private string _breed;

public string Name
{

get { return _name; }
set { _name = value; }
}

public string Breed
{
get { return _breed; }
set { _breed = value; }
}

public override string ToString()
{
return String.Format("Name: {0}, Breed: {1}", Name, Breed);
}

public Kennel();

public Kennel(string name, string breed)
{
this._name = name;
this._breed = breed;
}

#region #IComparable<Kennel> Members

public int CompareTo(Kennel other)
{
return other.Name.CompareTo(this.Name);
}

#endregion

}


class Run
{
public static void Main()
{
List<Kennel> kennels = new List<Kennel>();
kennels.Add(new Kennel("Alan", "French Bulldog"));
kennels.Add(new Kennel("Larry", "English Terrier"));
kennels.Add(new Kennel("Moe", "Gold Retriever"));
kennels.Add(new Kennel("Curly", "Chihuahua"));

foreach (Kennel k in kennels)
{
Console.WriteLine("Name: {0}, Breed: {1}", k.Name, k.Breed);
}


kennels.Sort();
foreach (Kennel k in kennels)
{
Console.WriteLine("Name: {0}, Breed: {1}", k.Name, k.Breed);
}


}
}

最佳答案

public Kennel();

这没有道理。

对于空的无参数构造函数,您仍然需要一个主体:
public Kennel()
{}

关于c# - C#运行时错误声明主体,因为它没有被标记为抽象,外部或局部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499201/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com