0
namespace APIproject    
{

    public class ApiCallResponse //class1

    class Program //class2
}

I have two classes in my code and both are in the same file name Program.cs. I want to shift my ApiCallResponse class into another file like Program1.cs And then I want to call the new file Program1.cs into Program.cs to access that class. How can I do that?

4
  • 2
    If you want to move the ApiCallResponse class to a new file, call the file ApiCallResponse.cs. Commented Jun 14, 2022 at 18:33
  • 1
    Have you tried making a second file? If so, what errors do you get? Commented Jun 14, 2022 at 18:34
  • 2
    Right click on the parent folder/object in Solution Explorer and select the option to Add and then select Class. Commented Jun 14, 2022 at 18:40
  • Add a new project in your solution, and uses the other project's namespace in the current project. Commented Jun 14, 2022 at 18:50

1 Answer 1

2

If you want to have a class in a separate file, which is actually a common practice, you need to add a class file (*.cs) and write your code there.

After this, you need to reference a namespace that contains your class and then use your class as usual:

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();
        }
    }
}

../Responses/ApiCallResponse.cs:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.