ููŠ ุงู„ู…ู‚ุงู„ ุฏู‡ุŒ ู‡ู†ุดุฑุญ ุงู„ู€ 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];
  1. ุงูุชุญ ุงู„ู€ Properties window. ุนุดุงู† ุชูุชุญู‡ุงุŒ ุฏูˆุณ ูƒู„ูŠูƒ ูŠู…ูŠู† ุนู„ู‰ ุงู„ู€ 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 ุฏู‡.