PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to split C# (Console application) classes into different files

 July 08, 2022     .net, c#, class, visual-studio     No comments   

Issue

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?


Solution

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



Answered By - Arthur Edgarov
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing