C# Program Arguments
Read the label's word from the command that starts Pine, not from a quote in the file. The checked command is dotnet run --no-build -- pine. The screen is still three lines: left=10 right=2 sum=12, then 2026-03-05, then pine x12. What changed is where pine lives.
Quality and Wrap-up continues, so Pine, Add, left, and right stay, and the plus still returns 12. The mini-project stored string name = "pine"; and the third line was pine x12 on every run. This page deletes that quote and reads args[0]. Until you build the new Main, dotnet run --no-build -- oak still prints pine x12, because the old program never looks at the word you typed. Build, and the same command prints oak x12.
The word after the dashes
Open the PineLab folder. Keep Add, its four /// lines, Label, the sum format, and new DateTime(2026, 3, 5). Delete string name = "pine";. Change the start method from Main() to Main(string[] args). Do not keep both. A class can have one entry point, and a second Main makes the build fail.
The parameter and the read:
public static void Main(string[] args)
string name = args[0];
args is the usual name for that parameter. string[] means a row of strings. The first word is index 0, not 1. args[0] is a string, the same kind Label already accepts. The call stays Console.WriteLine(Label(name, sum));.
From PineLab, run dotnet build. The report lists 0 errors and prints none of the three lines. Then run dotnet run --no-build -- pine.
using System;
public class Pine
{
/// <summary>Adds the two stored numbers.</summary>
/// <param name="left">The first number.</param>
/// <param name="right">The second number.</param>
/// <returns>The sum of left and right.</returns>
public static int Add(int left, int right)
{
return left + right;
}
public static string Label(string name, int count)
{
return name + " x" + count;
}
public static void Main(string[] args)
{
int left = 10;
int right = 2;
int sum = Add(left, right);
Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
DateTime day = new DateTime(2026, 3, 5);
Console.WriteLine("{0:yyyy-MM-dd}", day);
string name = args[0];
Console.WriteLine(Label(name, sum));
}
}
Same three lines, a different source
dotnet run --no-build -- pine shows left=10 right=2 sum=12, then 2026-03-05, then pine x12. Walk Main. Ten and two are still in the file. Add still returns 12. The date is still 5 March 2026. name is no longer a quoted word. It is whatever was typed after --. Label joins that word, a space, x, and the digits of sum.
Change the command to dotnet run --no-build -- oak. The third line becomes oak x12. The sum line and the date do not move. The word never enters Add. Put pine back when you want the checked screen.
Two different dashes sit in that command. --no-build is an option for dotnet: do not compile again. The bare -- after it ends dotnet's options. The word pine is for your program. Drop the bare --, as in dotnet run --no-build pine, and dotnet keeps the word. Main does not see pine, and you do not get the three lines from a successful argument run. Put the bare -- back in front of the word.
dotnet build on its own still prints no program line. Arguments do not apply to the build. They apply to the run.
How many words
args.Length is the number of words after --. The L is capital. args.length is Java's spelling, and this file is C#. A small l does not compile.
Put one print at the top of Main, before the sum:
Console.WriteLine(args.Length);
Build, then run dotnet run --no-build -- oak pine. The screen shows 2, then left=10 right=2 sum=12, then 2026-03-05, then oak x12. The length is 2 because two words followed --. args[0] is oak, so the label uses oak. args[1] is pine, and this demo does not print it. One word, dotnet run --no-build -- pine, prints 1 and then the three familiar lines, with the label pine x12.
args does not include the program name. The first user word is index 0. You do not skip a slot for PineLab.
This second shape is a count you try. The file you keep is the first one: no length line, one word, pine. Delete Console.WriteLine(args.Length); before you judge the checked screen.
using System;
public class Pine
{
/// <summary>Adds the two stored numbers.</summary>
/// <param name="left">The first number.</param>
/// <param name="right">The second number.</param>
/// <returns>The sum of left and right.</returns>
public static int Add(int left, int right)
{
return left + right;
}
public static string Label(string name, int count)
{
return name + " x" + count;
}
public static void Main(string[] args)
{
Console.WriteLine(args.Length);
int left = 10;
int right = 2;
int sum = Add(left, right);
Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
DateTime day = new DateTime(2026, 3, 5);
Console.WriteLine("{0:yyyy-MM-dd}", day);
string name = args[0];
Console.WriteLine(Label(name, sum));
}
}
No word stops after the date
On the file you keep, run dotnet run --no-build -- with no word after the bare --. Main starts. The sum line prints. The date prints. Then string name = args[0]; asks for a first word that is not there. The run stops, and pine x12 does not appear. Do not catch that stop on this page. Pass pine.
A space splits words. dotnet run --no-build -- oak pine is two arguments, and the label uses only args[0], so you see oak x12 even though you also typed pine. Quotes make one word that contains a space: dotnet run --no-build -- "oak pine" stores oak pine in args[0], and the label is oak pine x12. The checked command has no quotes and one word.
args[1] with only one word stops the run the same way a missing args[0] does. This page does not read index 1 in the file you keep.
An old build ignores the new word
Save the new Main and run dotnet run --no-build -- oak without dotnet build. You are running the mini-project program. That program still has string name = "pine"; and never reads args. The third line stays pine x12. The command looks new. The program is old. dotnet build, then run --no-build -- oak again. The third line becomes oak x12.
If the first line ends in 8, Add still has a minus. Arguments do not fix that. The label becomes pine x8 or oak x8 after a fresh build, because Label prints sum. Restore return left + right;, build, and use pine for the check.
Stay in PineLab, the folder with the .csproj. The class is Pine. The project name does not have to match. dotnet build builds the project, and the word after -- is not a project name.
Generics stays shut
You can take the word off the command line, keep the sum and the date, and see pine x12 when the word is pine. You can change only the label by typing oak. You can count the words with args.Length and then put that line away. The core path ends on this page. The next page is generics, and it is optional. You do not need it to finish the core path. This page stops before that topic. It does not add a type parameter, a list of labels, or a second project.
One last pass on the file you keep: one Main(string[] args), string name = args[0];, no quoted pine, no Random, no ReadLine, Add returns left + right, you are in PineLab, dotnet build succeeds, and dotnet run --no-build -- pine shows left=10 right=2 sum=12, then 2026-03-05, then pine x12. When those three lines match, program arguments did their job.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top