Passing multiple args of different types in C#

Can anyone please advise on how to pass two args of different types in a C# function? I have tried the following but it errors…

static void Main(string stringarg, int intarg)
{
foreach(string arg in args);
Console.WriteLine(stringargs);
Console.WriteLine(intarg);
Console.ReadKey();
}

I see a few problems in that code snippet.

  1. args does not exist - Not sure if its outside that method
  2. You do not have braces {} around your code that should be executed in the foreach loop, as I cannot see tabs that u might have.
  3. Console.WriteLine is trying to write stringargs, which doesnt exist. If that is in the foreach loop it should write arg.
  4. I have not done a console app in ages, but IIRC u cannot write out and int without the .ToString() method.

I hope this helps a bit

Sorry, here is the corrected code…

static void Main(string stringarg, int intarg)
{
foreach(string arg in stringarg);
Console.WriteLine(arg);
Console.WriteLine(intarg);
Console.ReadKey();
}

The compiler doesn’t throw an exception for the missing braces round the foreach condition, so if i remove the int intarg from the code and the line Console.WriteLine(intarg); the app runs ok and outputs the arg strings. If I reinput the integer argument in Main(string stringarg, int intarg) I get the error…“…<project.function>has the wrong signature to be an entry point” or "<project.function>"does not contain a static ‘main’ method suitable for an entry point…

When you create a windows console application the compiler will look for a method with the following signature as a starting point:

static {void or int} Main(string )

When you add another parameter to the Main you are changing it’s signature to

static void Main(string, int)

Thus the compiler can’t find a starting point which matches the signature it is looking for.

To get different types you can use the default Main with the string and parse the input to the desired type for example:

int IntArg = Int.Parse(args[0]);

You will want to make sure the input is numeric though before converting or you can use the Int.TryParse method.

Thank you for your explanation walbalooshi. You’ve confirmed what I thought - that the declaration was illegal, and given me a solution to the problem. However I am still baffled by the exercise instruction in the book I am learning from…

“Write an application that uses two command-line arguments to place values into a string and an integer variable, respectively. Then display these values.”

This seems to suggest that two separate arguments can be passed rather than isolating and converting part of a string array to int, or performing any parsing as per your suggestion. Do you think the instruction is misleading or perhaps it makes more sense to you than it does to me?

I believe they are talking about the arguments that you send the program through the command line when you run the program. Ex:

MyApplication.exe string 4

Now the string args parameter will contain two items

args[0] = “string”
args[1] = “4”

Now you can put the values in the args array into variables remembering that you need to parse the args[1] content to an Int with the function I showed above, then display the contents of the variables using the Console.Write or .WriteLine methods

Yup, the only “entree” point is the string args. You can extract just about anything you’d want from there at the end of the day.

Yes, not using braces in C# is perfectly legal, but I would strongly suggest using them and it just keeps things clean when you have a lot of code as it is a lot easier to see exactly whats in the if or loop.

Anywayz… enough about that. command line args are usually all string, so I would assume they want you to know that you need to convert it to int, but, at the same time, ur writing it out without doing anything with it, so no actual need to convert to int, but it is the basics that you will need to know later for when you actually want to work with the int

Ok thanks everyone that’s as covered that off then, to complete the exercise a second string array command line arg must be parsed to int.