0

I have following code in C# :

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello...");
        }
    }

In function Void Main what is mean of string[] args what is the use of string[] args in Program ??

Thanks

2
  • you could have found answer much quicker if you had searched for it on google rather than posting the question here ;) Commented Aug 19, 2013 at 4:15
  • 1
    see the MSDN documentation msdn.microsoft.com/en-us/library/vstudio/acy3edy3.aspx Commented Aug 19, 2013 at 4:15

1 Answer 1

7

The string[] args may contain any number of command line arguments which we want to pass to Main() method.

If we were executing the application through command prompt we could see how it would work.

For a method as shown

static int Main(string[] args)
{

  for(int i = 0; i < args.Length; i++)
  Console.WriteLine("Arg: {0}", args[i]);
  Console.ReadLine();
  return -1;
}

enter image description here

For example, you can pass a FileName and access it while the application starts running. Suppose if the application is a text editor we can open the text file like this.

The Main method can be declared with or without a string[] parameter that contains command-line arguments. When using Visual Studio to create Windows Forms applications, you can add the parameter manually or else use the Environment class to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument.

For more details please refer here

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.