C#:如何执行它们,以及对控制台的要求?

huangapple go评论109阅读模式
英文:

C#: How to execute them, with the requirements on Console?

问题

我需要在文件内编写C#代码并运行它们。
但在运行时的问题是文件名应该是"MAIN",或者文件内的类应该与文件名相同。
两者都不能同时实现,但我需要练习运行它们。

是否有一种方式,就像在Python中我们使用命令python fileName.py来运行程序一样,我们不能在这里做类似的事情吗?它还说要构建项目,但无法为每个1KB的文件都构建。

以下是文件名,它们是我应该练习并运行的问题。

[nitkarshc@nitkarshC C#]$ ls
Easy  Expert  Hard  Medium  README.md  veryEasy  veryHard
[nitkarshc@nitkarshC C#]$ cd veryEasy/
[nitkarshc@nitkarshC veryEasy]$ ls -1v
Main.cs
tempCodeRunnerFile.cs
'[1] Return the Sum of Two Numbers [VE].cs'
'[2] Convert Minutes into Seconds [VE].cs'
'[3] Return the Next Number from the Integer Passed [VE].cs'
'[4] Circuit Power Calculator [VE].cs'
'[5] Convert Age to Days [VE].cs'
...

我尝试运行它们。
但错误是,Main类不能与不同的文件名一起运行,反之亦然。
我在同一文件夹中有多个文件,即使我构建一个项目并以某种方式运行它们,它也说只能有一个与主类相关的入口点。
我该如何执行,就像Python一样?

英文:

I needed to write C# code inside the file and run them.
But the problem while running is the file name should be MAIN or the class inside should be equivalent to the file name.
Can't do both, but need to run them for practicing.

Is there no way just like in python we run using the command python fileName.py and it runs the program, so can't we do something similar here, it says to build project too, but can't build for every single 1kb file.

The file names are below, they are problems which I should practice on and run them.

[nitkarshc@nitkarshC C#]$ ls
Easy  Expert  Hard  Medium  README.md  veryEasy  veryHard
[nitkarshc@nitkarshC C#]$ cd veryEasy/
[nitkarshc@nitkarshC veryEasy]$ ls -1v
Main.cs
tempCodeRunnerFile.cs
'[1] Return the Sum of Two Numbers [VE].cs'
'[2] Convert Minutes into Seconds [VE].cs'
'[3] Return the Next Number from the Integer Passed [VE].cs'
'[4] Circuit Power Calculator [VE].cs'
'[5] Convert Age to Days [VE].cs'
'[6] Area of a Triangle [VE].cs'
'[7] Return the Remainder from Two Numbers [VE].cs'
'[8] Is the Number Less than or Equal to Zero_ [VE].cs'
'[9] Less Than 100_ [VE].cs'
'[10] Are the Numbers Equal_ [VE].cs'
'[11] Return Something to Me! [VE].cs'
'[12] Flip the Boolean [VE].cs'
'[13] Convert Hours into Seconds [VE].cs'
'[14] Sum of Polygon Angles [VE].cs'
'[15] Are the Numbers Equal_ [VE].cs'
'[16] Basic Variable Assignment [VE].cs'
'[17] Using the _&&_ Operator [VE].cs'
'[18] Basketball Points [VE].cs'
'[19] Find the Perimeter of a Rectangle [VE].cs'
'[20] Name Greeting! [VE].cs'
'[21] The Farm Problem [VE].cs'
'[22] Football Points [VE].cs'
'[23] Return the First Element in an Array [VE].cs'
'[24] Buggy Code (Part 4) [VE].cs'
'[25] Convert Hours and Minutes into Seconds [VE].cs'
'[26] Maximum Edge of a Triangle [VE].cs'
'[27] Inches to Feet [VE].cs'
'[28] Flip the Integer Boolean [VE].cs'
'[29] Check if an Integer is Divisible By Five [VE].cs'
'[30] Correct the Mistakes [VE].cs'
[nitkarshc@nitkarshC veryEasy]$

I tried running them.
But error is like, something Main class can't be run with different file name or vice - versa.
I have multiple files on the same folder, even if I build a project and somehow run them. It says only one entry can only be there, with regards to main.
How can I execute like python?

答案1

得分: 1

你现在遇到的主要问题是,C#的行为与Python有很大的不同。你没有编译和运行单个.cs文件的选项,你需要先创建一个项目。

如果你正在使用.NET CLI工具,只需要运行以下命令:dotnet new console -n "My application"。这将创建一个新的控制台应用程序。然后,你可以在生成的.cs文件中实现你的程序,或者在项目目录中创建新的类,然后在程序中引用这些类并从那里运行它们。

要构建和运行应用程序,你可以使用dotnet run命令,它将自动构建并运行你的程序。

英文:

The main problem you're having is that C# behaves a lot differently than python. You don't have an option to compile and run singular .cs files. You need to create a project first.

If you're using the .NET CLI tool it's just this command: dotnet new console -n "My application". This will create a new console application. From that you can just implement in the generated .cs file your program, or just create new classes in the project directory and you can reference those classes in the program and run them from there.

To build and run the app you can use the dotnet run command which will automatically build and run your program.

huangapple
  • 本文由 发表于 2023年6月15日 14:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定