Learn Command Line Arguments in C# with Easy Examples
👋 Introduction
Hey there! 👋 We all know that we can pass parameters to a function as arguments. But have you ever wondered about the Main(string[] args)
method? Can we pass parameters to the Main()
method in C#? 🤔 The answer is yes! You can pass parameter(s) to the Main()
method, and this is known as command line arguments.
The Main()
method is the starting point where a C# program begins its execution. However, it doesn’t accept parameters from other methods. Instead, it receives parameters directly from the command line. These parameters are passed as an array, which means it can hold any number of values at runtime! 🎯
In Main(string[] args)
, the args
is simply a string array that can store multiple parameters provided when you run the program. Pretty cool, right? 😎
📚 What You Are Going to Learn in This Lesson:
✔️ What are Command Line Arguments in C#?
✔️ How to use them in your programs with simple syntax
✔️ Real-world scenarios to understand where and why to use them
✔️ Complete code examples with outputs and detailed explanations
✔️ How to handle multiple arguments and common use cases
💡 1. What are Command Line Arguments in C#?
Command Line Arguments in C# are values you pass to your program when you run it. They help you control the program without changing the code. Pretty neat, right?
📝 Syntax:
static void Main(string[] args)
{
// args is an array containing command line arguments
}
Here, args
is an array of strings that stores the arguments you pass.
🚀 Example 1: Basic Command Line Arguments
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Number of arguments: " + args.Length);
foreach (string arg in args)
{
Console.WriteLine("Argument: " + arg);
}
}
}
🖥️ How to Run:
In your terminal or command prompt:
dotnet run arg1 arg2 arg3
🖥️ Output:
Number of arguments: 3
Argument: arg1
Argument: arg2
Argument: arg3
🔍 Explanation:
args.Length
shows how many arguments you passed.- The
foreach
loop prints each argument.
😎 Simple, right? No complex code—just straightforward input!
🌍 Real-World Example 1: Greeting Users
Let’s make a program that greets users by their name using Command Line Arguments in C#.
using System;
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine($"Hello, {args[0]}!");
}
else
{
Console.WriteLine("Hello, Friend!");
}
}
}
🖥️ How to Run:
In your terminal or command prompt:
dotnet run Steven
🖥️ Output:
Hello, Steven!
If you run it without arguments:
Hello, Friend!
💡 Why this helps:
Perfect for personalization! Imagine an app welcoming users with their names. Feels special, right? 😊
🧮 Example 2: Simple Calculator Using Arguments
Let’s build a basic calculator that adds two numbers passed through the command line. 🧮
using System;
class Program
{
static void Main(string[] args)
{
if (args.Length == 2)
{
int num1 = int.Parse(args[0]);
int num2 = int.Parse(args[1]);
int sum = num1 + num2;
Console.WriteLine($"Sum: {sum}");
}
else
{
Console.WriteLine("Please provide two numbers.");
}
}
}
🖥️ How to Run:
In your terminal or command prompt:
dotnet run 5 7
🖥️ Output:
Sum: 12
🔍 Explanation:
args[0]
andargs[1]
take the numbers.- We convert them from strings to integers with
int.Parse()
. - Finally, we display their sum.
🙌 Quick calculations made easy—no GUI required!
🎛️ Example 3: Temperature Converter 🌡️
Let’s build a temperature converter that converts Celsius to Fahrenheit using a command line argument.
using System;
class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
{
double celsius = double.Parse(args[0]);
double fahrenheit = (celsius * 9 / 5) + 32;
Console.WriteLine($"{celsius}°C = {fahrenheit}°F");
}
else
{
Console.WriteLine("Please provide the temperature in Celsius.");
}
}
}
🖥️ How to Run:
In your terminal or command prompt:
dotnet run 25
🖥️ Output:
25°C = 77°F
🔥 Quick conversions without any hassle! Perfect for your weather app or science projects. 🌦️
🏁 Conclusion
Yay! 🎉 You’ve just learned how to use Command Line Arguments in C# like a pro! Whether you’re greeting users, reading files, or building apps, command line arguments make your programs more flexible and interactive. 🌟
So, how’s it going so far? Having fun? Coding can be challenging at times, but you’re doing great! 💪 Keep experimenting—try changing the arguments and see what happens. The best way to learn is to play around and have fun! 😎
🚀 Next what?
Up next: Best Practices and Common Mistakes when using parameters in C#! Want to avoid silly errors and write cleaner code? Stay tuned—it’s going to be super helpful! See you there! 👋