You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何提示参数无对应实参?C# CS7036错误求助

解决C#错误CS7036:不存在与Book构造函数所需形参'keywords'对应的实参

错误原因

你的Book类定义了两个构造函数:

  • 无参构造函数Book()
  • 需传入5个参数的构造函数Book(string author, string title, string keywords, string publicationDate, int isbn)

但在主代码实例化Book时,你只传入了2个参数:

Book newBook = new Book("Orson Scott Card, Enders Game, Science Fiction, January 15th 1985", 0812550706);

编译器找不到匹配的构造函数,因此抛出CS7036错误。

修改方案

方案1:正确调用现有5参数构造函数

将主代码中的实例化语句改为分别传入每个字段的具体值,同时注意ISBN不能以0开头(C#中以0开头的整数会被解析为八进制,而八进制数不能包含8):

static void Main(string[] args)
{
    Book newBook = new Book(
        "Orson Scott Card", 
        "Enders Game", 
        "Science Fiction", 
        "January 15th 1985", 
        812550706
    );
    Console.WriteLine(newBook.ToString());
}

方案2:添加匹配的2参数构造函数

如果你希望保持传入一个逗号分隔字符串和ISBN的方式,可以在Book类中新增对应的构造函数,将字符串拆分后赋值给各个字段:

public class Book
{
    // 原有字段、无参构造函数、属性不变...

    // 新增2参数构造函数
    public Book(string bookInfo, int isbn)
    {
        // 按", "拆分字符串,注意匹配输入的分隔格式
        string[] infoParts = bookInfo.Split(new[] { ", " }, StringSplitOptions.None);
        
        // 赋值字段,处理拆分失败的边界情况
        _author = infoParts.Length > 0 ? infoParts[0] : "";
        _title = infoParts.Length > 1 ? infoParts[1] : "";
        _keywords = infoParts.Length > 2 ? infoParts[2] : "";
        _publicationDate = infoParts.Length > 3 ? infoParts[3] : "";
        _isbn = isbn;
    }

    // 原有ToString方法可优化为字符串插值(可选)
    public override string ToString()
    {
        return $"AUTHOR: {Author} TITLE: {Title} KEYWORDS: {Keywords} PUBLICATION DATE: {PublicationDate} ISBN: {ISBN}";
    }
}

添加后,你原来的主代码即可正常运行。

内容的提问来源于stack exchange,提问作者Taylor Layton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 06:15:33