Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In the main method of a C# console application usually we use string as parameter (
C#
static void Main(string[] args)

)
Can we use other types such as Integer as parameter of main method. Besides can we use more than one parameter is main method?

What I have tried:

static void Main(int[] args){
}
Posted
Updated 13-Apr-16 4:11am

You can have as many parameters to Main as you like, that is why it is defined as a string[] rather than string.

Although all of the arguments are passed into Main as strings, they can be converted to other types. See the reference Command-Line Arguments (C# Programming Guide)[^]
 
Share this answer
 
Comments
Member 11345334 13-Apr-16 8:59am    
Actually i want to take an image as input(from my desktop) from command prompt through the main method. How can i do that?
jimmson 13-Apr-16 9:09am    
Pass path to image as argument and load image in code.
CHill60 13-Apr-16 9:10am    
What ^^he^^ said
Sergey Alexandrovich Kryukov 13-Apr-16 9:29am    
5ed, but the request still makes practical sense, if it's reformulated accurately. Please see Solution 2.
—SA
Quote:
Can we use other types such as Integer as parameter of main method. Besides can we use more than one parameter is main method?

You can only have string type parameters, it is your task to convert strings as you need.
A simple space is needed to separate parameters.
 
Share this answer
 
Do you understand the purpose of command line; and why it's represented in array of strings, to your application?

It's done by historical reasons, because a string is a universal way to pass data. This is what it is, you cannot change it.

In practice "other types" means string using string (or strings) but trying to parse it according some application schema.

Please see my article where I present an easy-to-use facility of this kind: Enumeration-based Command Line Utility[^].

Another alternative, also a CodeProject work, is referenced in my article.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900