如何将c#(控制台应用程序)类拆分到不同的文件中


namespace APIproject    
{
public class ApiCallResponse //class1
class Program //class2
}

我的代码中有两个类,它们都在相同的文件名Program.cs中。我想转移我的ApiCallResponse类到另一个文件,如Program1.cs然后我要把新文件program1。cs调用到program1。cs来访问那个类。我该怎么做呢?

如果您想在单独的文件中拥有一个类,这实际上是一种常见的做法,您需要添加一个类文件(*.cs)并在那里编写代码。

之后,您需要引用包含您的类的名称空间,然后像往常一样使用您的类:

Program.cs:

using MySolution.Responses; //This is how you can connect different classes.
namespace MySolution
{
public class Program
{
public static void Main(string[] args)
{
var response = new ApiCallResponse();
}
}
}

. ./回复/ApiCallResponse.cs:

namespace MySolution.Responses
{
public class ApiCallResponse
{
public ApiCallResponse()
{

}
}
}

最新更新