C#分部类调用异常:无法正确执行第二个类中的方法
问题分析与解决:分部类调用方法无响应
你的问题和**分部类(partial class)**毫无关系,核心原因是Console.ReadLine()的工作机制:
DoSomething()方法里的Console.ReadLine()会让程序暂停执行,等待你在控制台输入任意内容并按下回车键,之后才会继续执行后续代码。- 你看到的“无响应”只是程序在等待输入,从始至终只有一个控制台窗口,不存在“关闭第一个控制台才能打开第二个”的情况。
两种解决方式:
- 不需要等待输入,直接输出内容:去掉两个方法里的
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(); // 防止程序执行完直接退出 } }
- 需要等待用户确认后再执行下一步:保留
Console.ReadLine(),在第一个方法输出后,在控制台输入任意内容并回车,程序就会继续执行AndAgainDoSomething()方法,输出第二行内容。
内容的提问来源于stack exchange,提问作者Yul P
相关产品推荐
相关产品推荐

