ูู ุงูู ูุงู ุฏูุ ููุดุฑุญ ุงูู Command Line Arguments ูู ูุบุฉ C# ูููุดูู ุฃู ุซูุฉ ุนู ููุฉ ุนูููุง.
Understanding Command-Line Arguments
ุฅุญูุง ุนุงุฑููู ุฅููุง ู
ู
ูู ูู
ุฑุฑ parameters ูุฃู function ูู argumentุ ุจุณ ูู ู
ู
ูู ูุนู
ู ููุณ ุงูููุงู
ู
ุน ุงูู Main(string[] args) methodุ
ุงูุฅุฌุงุจุฉ ูู ุฃููุฉุ ู
ู
ูู. ูุฏู ุจูุชู
ุนู ุทุฑูู ุงููู ุจูุณู
ูู Command-Line Arguments. ุจุจุณุงุทุฉุ ุฏู ุงูู arguments ุงููู ุงูููุฒุฑ ุฃู ุงูู
ุจุฑู
ุฌ ุจูู
ุฑุฑูุง ููู Main() method ููุช ุชุดุบูู ุงูุจุฑูุงู
ุฌ.
ุงูู Main() method ูู ููุทุฉ ุงูุจุฏุงูุฉ ุงููู ุจูุจุฏุฃ ู
ู ุนูุฏูุง ุชูููุฐ ุฃู ุจุฑูุงู
ุฌ. ููุทุฉ ู
ูู
ุฉ ูุงุฒู
ุชุงุฎุฏ ุจุงูู ู
ููุง ูู ุฅู ุงูู Main() method ู
ุด ุจุชูุจู ุฃู parameters ู
ู ุฃู method ุชุงููุฉ ุฌูู ุงูููุฏุ ูู ุจุชูุจู parameters ู
ู ุงูู Command-Line ุจุณ.
ูู ุจุตูุช ุนูู ุงูู signature ุจุชุงุน ุงูู Main() methodุ ูุชูุงูู ุฅููุง ุจุชุงุฎุฏ parameter ู
ู ููุน string array ุงุณู
ู args. ุงูู array ุฏู ููุฏุฑ ูุณุชูุจู ุฃู ุนุฏุฏ ู
ู ุงูู arguments ููุช ุชุดุบูู ุงูุจุฑูุงู
ุฌ (runtime).
Passing Arguments via Visual Studio
ุฎูููุง ูุนู
ู console application ุฌุฏูุฏ ููุนุฏูู ู
ูู Program.cs ุจุงูููุฏ ุฏู:
Code Example: Accessing String Arguments
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
// Print the first argument
Console.WriteLine($"First Command Line Argument {args[0]}");
// Print the second argument
Console.WriteLine($"Second Command Line Argument {args[1]}");
// Print the third argument
Console.WriteLine($"Third Command Line Argument {args[2]}");
Console.ReadLine();
}
}
}ุงูููุฏ ุงููู ููู ุฏู ู
ุชููุน ุฅููุง ูู
ุฑุฑ ูู ุนูู ุงูุฃูู 3 parameters ููู Main() method.
Potential Errors: IndexOutOfRangeException
ูู ุดุบููุช ุงูุจุฑูุงู
ุฌ ุฏูููุชูุ ููุญุตู runtime exception ู
ู ููุน System.IndexOutOfRangeException ูุฑุณุงูุชู ูุชููู: โIndex was outside the bounds of the arrayโ.
ูุฏู ู
ูุทูู ุฌุฏูุงุ ูุฅููุง ู
ุดุบููุงุด ุงูุจุฑูุงู
ุฌ ูู
ุนุงู ุฃู argumentsุ ูุจุงูุชุงูู ุงูู string array ุงููู ุงุณู
ู args ูุงุถู ูู
ููููุด ุฃู ุนูุงุตุฑุ ูุฅุญูุง ุจูุญุงูู ููุตู ูุนูุงุตุฑ ู
ุด ู
ูุฌูุฏุฉ ููู ุฃุณุงุณูุง.
ุงูุณุคุงู ุฏูููุชูุ ุฅุฒุงู ูู
ุฑุฑ arguments ููู Main() methodุ ุงูุฅุฌุงุจุฉ ูู ุนู ุทุฑูู ุงูู Command-Line.
How to Set Arguments in Visual Studio
ุนุดุงู ูู
ุฑุฑ ุงูู arguments ู
ู ุฎูุงู Visual Studioุ ุงุชุจุน ุงูุฎุทูุงุช ุฏู:
graph TD A[Right-click on the project in Solution Explorer] --> B[Click on Properties]; B --> C[Select the Debug tab]; C --> D[Enter values in the 'Command line arguments' textbox];
- ุงูุชุญ ุงูู
Propertieswindow. ุนุดุงู ุชูุชุญูุงุ ุฏูุณ ูููู ูู ูู ุนูู ุงููprojectุจุชุงุนู ูู ุงููSolution ExplorerูุงุฎุชุงุฑProperties.
2. ู
ู ุดุงุดุฉ ุงูู Propertiesุ ุงุฎุชุงุฑ ุงูู Debug tab.
3. ูู ุงูู Command line arguments textboxุ ุงูุชุจ ุงูููู
ุงููู ุนุงูุฒ ุชู
ุฑุฑูุง ููู Main() methodุ ูุฎูู ุจูููู
ู
ุณุงูุฉ. ูู ู
ุซุงููุงุ ุฅุญูุง ู
ุญุชุงุฌูู 3 ููู
ุ ููููุชุจ ู
ุซููุง: Value1 Value2 Value3.

Output
ููุงุ Value1 ูุชุชุฎุฒู ูู args[0]ุ ูValue2 ูุชุชุฎุฒู ูู args[1]ุ ูValue3 ูุชุชุฎุฒู ูู args[2].
ุฏูููุชู ุงุนู
ู Save ูุดุบูู ุงูุจุฑูุงู
ุฌุ ูุชูุงูู ุงููุชูุฌุฉ ุฏู ุธูุฑุช ูู ุงูู Console:
First Command Line Argument Value1
Second Command Line Argument Value2
Third Command Line Argument Value3
Important Points
- ุงูู
Command Line Argumentsุจุชุณุชูุจููุง ููstring arrayุ ุงููู ูู ุงููparameterุงููู ุงุณู ูargsูู ุงููMain()method. - ุจุดูู ุนุงู
ุ ุงูู
Command Line Argumentsุจุชูุณุชุฎุฏู ุนุดุงู ุชุญุฏุฏ ู ุนููู ุงุช ุฎุงุตุฉ ุจุงููconfigurationููุช ุชุดุบูู ุงูุจุฑูุงู ุฌ. - ุงูู
ุนููู
ุงุช ุฏู ุฏุงูู
ูุง ุจุชุชุจุนุช ุนูู ููุฆุฉ
string. - ู
ููุด ุฃู ูููุฏ ุนูู ุนุฏุฏ ุงูู
argumentsุ ู ู ูู ุชู ุฑุฑ ุตูุฑ ุฃู ุฃู ุนุฏุฏ (n) ู ู ุงููarguments.
Handling Numeric Arguments
ูู C#ุ ุงูู Command Line Arguments ุฏุงูู
ูุง ุจุชุชุฎุฒู ูู string ูุจุชููู ู
ูุตููุฉ ุจู
ุณุงูุงุช. ุงูู Main() method ู
ุด ุจุชูุจู ุบูุฑ string arguments.
ุทูุจ ูู ุงูุจุฑูุงู
ุฌ ู
ุญุชุงุฌ ูุชุนุงู
ู ู
ุน numeric command-line argumentุ ุงููู ุนููู ุชุนู
ูู ุฅูู ุชู
ุฑุฑ ุงูุฑูู
ูู string ุนุงุฏูุ ูุฌูู ุงูุจุฑูุงู
ุฌ ุจุชุงุนูุ ู
ุณุฆูููุชู ุฅูู ุชุญููู ุงูู string ุฏู ูููู
ุฉ ุฑูู
ูุฉ. ุจูุฏูุ ุจููุฏุฑ ูุชุนุงู
ู ู
ุน ุฃุฑูุงู
ุนู ุทุฑูู ุงูู command line.
Code Example: Converting to Numeric Types
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
// Convert the first argument to an integer
int argument1 = Convert.ToInt32(args[0]);
Console.WriteLine("Argument in Integer Form : " + argument1);
// Convert the second argument to a double
double argument2 = Convert.ToDouble(args[1]);
Console.WriteLine("Argument in Double Form : " + argument2);
Console.ReadLine();
}
}
}ุฏูููุชูุ ุนุฏูู ุงูููู
ูู ุดุงุดุฉ Properties โ Debug ูุฎูููุง ุฃุฑูุงู
ุ ุฒู ูุฏู: 100 250.75
ุงุนู
ู Save ูุดุบูู ุงูุจุฑูุงู
ุฌุ ูุงููุชูุฌุฉ ูุชููู:
Argument in Integer Form : 100
Argument in Double Form : 250.75
Potential Errors: FormatException
ุทุจ ุฅูู ุงููู ููุญุตู ูู ุงูู argument ุงููู ุงุชุจุนุช ู
ูููุนุด ูุชุญูู ููููุน ุงูุฑูู
ู ุงูู
ุทููุจุ
ูู ุงูู argument ู
ูุฏุฑุด ูุชุญููุ ููุญุตู runtime error ู
ู ููุน System.FormatException ูุฑุณุงูุชู ูุชููู: โInput string was not in a correct format.โ
ุฎูููุง ูุบูุฑ ุงูู command line arguments ููุฎูููุง ุจุงูุดูู ุฏู: 100 Hello. ููุง ุงูู argument ุงูุชุงูู ููุนู string ูู
ูููุนุด ูุชุญูู ูู double.
ูู ุนู
ูุช Save ูุดุบููุช ุงูุจุฑูุงู
ุฌุ ููุธูุฑูู ุงูู runtime error ุฏู.