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

C#分部类调用异常:无法正确执行第二个类中的方法

问题分析与解决:分部类调用方法无响应

你的问题和**分部类(partial class)**毫无关系,核心原因是Console.ReadLine()的工作机制:

  • DoSomething()方法里的Console.ReadLine()会让程序暂停执行,等待你在控制台输入任意内容并按下回车键,之后才会继续执行后续代码。
  • 你看到的“无响应”只是程序在等待输入,从始至终只有一个控制台窗口,不存在“关闭第一个控制台才能打开第二个”的情况。

两种解决方式:

  1. 不需要等待输入,直接输出内容:去掉两个方法里的Console.ReadLine(),如果想让程序执行完后不立刻关闭窗口,可以在Main方法最后加一个Console.ReadLine():
public interface ISomething
{
}

public partial class MyInterface : ISomething
{
    public void DoSomething()
    {
        Console.WriteLine("Thats pt1 of what my partial class is doing ");
    }
}

public partial class MyInterface
{
    public int MyProperty { get; set; }
    public int MyProperty1 { get; set; }

    public void AndAgainDoSomething()
    {
        Console.WriteLine("Thats pt2 of what my partial class is doing ");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var firstPart = new MyInterface();
        firstPart.DoSomething();
        firstPart.AndAgainDoSomething();
        Console.ReadLine(); // 防止程序执行完直接退出
    }
}
  1. 需要等待用户确认后再执行下一步:保留Console.ReadLine(),在第一个方法输出后,在控制台输入任意内容并回车,程序就会继续执行AndAgainDoSomething()方法,输出第二行内容。

内容的提问来源于stack exchange,提问作者Yul P

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 06:01:46