可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This would be question from c# beginner.
When I create console application I get Main method with parameter args as array string.
I do not understand how this method is called by system and how args are passed to the Main method. Maybe someone could explain? Is Main method are overridden of some kind of console class?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
回答1:
The Main method is the Entry point of your application. If you checkout via ildasm
then
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
This is what helps in calling the method
The arguments are passed as say C:\AppName arg1 arg2 arg3
回答2:
All answers are awesome and explained everything very well
but I just want to point out differant way for passing args
to main method
in visual studio
right click on project
then choose Properties
- go to
debug tab
then on the start options
section provide the app with your args
like this image
and happy knowing secrets
回答3:
The runtime splits the arguments given at the console at each space.
If you call
myApp.exe arg1 arg2 arg3
The Main Method gets an array of
var args = new string[] {"arg1","arg2","arg3"}
回答4:
How is main called?
When you are using the console application template the code will be compiled requiring a method called Main in the startup object as Main is market as entry point to the application.
By default no startup object is specified in the project propery settings and the Program class will be used by default. You can change this in the project property under the "Build" tab if you wish.
Keep in mind that which ever object you assign to be the startup object must have a method named Main in it.
How are args passed to main method
The accepted format is MyConsoleApp.exe value01 value02 etc...
The application assigns each value after each space into a separate element of the parameter array.
Thus, MyConsoleApp.exe value01 value02 will mean your args paramter has 2 elements:
[0] = "value01"
[1] = "value02"
How you parse the input values and use them is up to you.
Hope this helped.
Additional Reading:
Creating Console Applications (Visual C#)
Command-Line Arguments (C# Programming Guide)
回答5:
in visual studio you can also do like that to pass simply or avoiding from comandline argument
static void Main(string[] args)
{
if (args == null)
{
Console.WriteLine("args is null"); // Check for null array
}
else
{
args=new string[2];
args[0] = "welcome in";
args[1] = "www.overflow.com";
Console.Write("args length is ");
Console.WriteLine(args.Length); // Write array length
for (int i = 0; i < args.Length; i++) // Loop through array
{
string argument = args[i];
Console.Write("args index ");
Console.Write(i); // Write index
Console.Write(" is [");
Console.Write(argument); // Write string
Console.WriteLine("]");
}
}
回答6:
Command line arguments is one way to pass the arguments in. This msdn sample is worth checking out.
The MSDN Page for command line arguments is also worth reading.
From within visual studio you can set the command line arguments by Choosing the properties of your console application then selecting the Debug tab
回答7:
Every managed exe has a an entry point which can be seen when if you load your code to ILDASM. The Entry Point is specified in the CLR headed and would look something like this.
回答8:
Read MSDN.
it also contains a link to the args.
short answer:
no, the main does not get override. when visual studio (actually the compiler) builds your exe it must declare a starting point for the assmebly, that point is the main function.
if you meant how to literary pass args then you can either run you're app from the command line with them (e.g. appname.exe param1 param2) or in the project setup, enter them (in the command line arguments in the Debug tab)
in the main you will need to read those args for example:
for (int i = 0; i < args.Length; i++)
{
string flag = args.GetValue(i).ToString();
if (flag == "bla")
{
Bla();
}
}
回答9:
The args can be passed via command line and there can be no args as well.
回答10:
The main method of the runtime engine looks something like int main(int argc, char *argv[])
, where argc is a count of the number of arguments and argv is an array of pointers to each. The runtime engine converts this into a form that is more natural to c#.
Prior to that main method being called, everything is in assembly language. It has access to the command line arguments (because the operating system makes that available to every process that starts), but that assembly language needs to convert a single string of the full command line into multiple substrings (using whitespace to separate them) before it's ready to pass them into main().
回答11:
you can pass also by making function and then in this function you call main method and pass argument to main method
static int Main(string[] args)
{
foreach (string b in args)
Console.WriteLine(b+" ");
Console.ReadKey();
aa();
return 0;
}
public static void aa()
{
string []aaa={"Adil"};
Console.WriteLine(Main(aaa));
}