user3582190
user3582190

Reputation: 35

How to Run c# application through Command Prompt?

I have Two classes. Test.cs, Entity.cs.

I have put this two files in one folder "CommandTest".

*Test.cs Class

namespace Demo
{
 public class Test
{
 public static Entity entity= new Entity();
 public static void Main(string[] args)
{
Console.WriteLine("Demo");
}

*Entity.cs class

namespace Demo
{
public class Entity
{
Console.WriteLine("entity");
}
}

When I am Trying to run it through "Visual Studio Command Prompt". It shows Error,

Test.cs(10,28): error CS0234: The type or namespace name 'Entity' does not exist in the namespace 'Demo' (are you missing an assembly reference?)

I am not getting why it is shows error. Because both class have same namespace. How can I run it through Command Prompt.

Thanks.

Upvotes: 1

Views: 4264

Answers (2)

Zohaib Aslam
Zohaib Aslam

Reputation: 595

you need to provide name of all classes explicitly. so in your case you will execute

csc.exe /out:ExecutableName.exe Entity.cs Test.cs

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157098

You should use all files when calling csc. It doesn't try to find the code files itself. Try this:

csc /out:Test.exe Test.cs Entity.cs

Or, maybe easier:

csc /out:Test.exe *.cs

Also, read the related MSDN article.

Don't forget to add this code block in a method too:

namespace Demo
{
    public class Entity
    {
        public void SomeMethod() /* here */
        {
            Console.WriteLine("entity");
        }
    }
}

Upvotes: 2

Related Questions