Home » .Net

Command line argument in C# with Example

Learn: What are Command Line Arguments in C#.Net with Example? Which is used to pass messages while calling the executable file from command line.

Command line argument is very important concept in every programming language. Using this we can pass arguments to main function at runtime. As we know that main function contains arguments of string array. It can accept command line argument from console.

command line arguments example in C#.Net

Consider the program:

using System;

class Sample
{
	static void Main(string[] args)
	{
		int SUM	= 0;
		int X	= 0;
		int Y	= 0;

		X = Convert.ToInt32(args[0]);
		Y = Convert.ToInt32(args[1]);

		SUM = X + Y;

		Console.WriteLine("Sum is: "+SUM);
		
	}
}

Output

D:\>sample 10 20
Sum is: 30

In this program, args of main function accepts argument values from console when we execute the program.

Here, we are passing 10 and 20 as command line arguments which can be accessed through args. Where 10 will be assigned into args[0] and 20 will be assigned into args[1].






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.