Library tutorials & articles
Program Entry point in C#
By Kamran Shakil, published on 13 Mar 2002
Page 2 of 5
- Introduction
- Example 1 & 2
- Example 3 & 4
- More Examples
- Additional Examples
Example 1 & 2
Copy as a command takes two parameters; the source and destination files. Similarly,
a C# program can also accept command line parameters at runtime. The executable
kamran.exe can be entered as
kamran.cs
Output
one, two, three are called command line parameters. The program accepts them in an array of strings. As the array is a parameter to the function, we are free to decide on its name. Every array has a member called Length, which tells us the size of the array. In our case, it is three. Thus, a[0] will contain the first word one and not the name of the program, a[1] will contain two and a[3] - three. Main now behaves like any other function. What we do next with the command line arguments depends entirely upon us.
kamran.cs
Compiler Error
You can have one and only one function called Main in any C# program. Even though you can call it with different parameters, with the name changing, Main as a function must be given only once.
C:\>kamran one two three
kamran.cs
class zzz
{
public static void Main(string[] a)
{
int i;
for ( i=0; i< a.Length; i++)
System.Console.WriteLine(a[i]);
}
}
Output
one
two
three
one, two, three are called command line parameters. The program accepts them in an array of strings. As the array is a parameter to the function, we are free to decide on its name. Every array has a member called Length, which tells us the size of the array. In our case, it is three. Thus, a[0] will contain the first word one and not the name of the program, a[1] will contain two and a[3] - three. Main now behaves like any other function. What we do next with the command line arguments depends entirely upon us.
kamran.cs
class zzz
{
public static void Main(string[] a)
{
}
public static int Main()
{
}
}
Compiler Error
kamran.cs(3,20): error CS0017: Program 'kamran.exe' has
more than one entry point defined: 'zzz.Main(string[])'
kamran.cs(6,19): error CS0017: Program 'kamran.exe' has more than one entry point
defined: 'zzz.Main()'
You can have one and only one function called Main in any C# program. Even though you can call it with different parameters, with the name changing, Main as a function must be given only once.
Related articles
Related discussion
-
Help please for student
by mandy130 (0 replies)
-
Loop help needed
by BKRoberts (5 replies)
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
Regarding User control creation in C# .Net(Windows Application)
by porchelvi (0 replies)
-
Access Database Refreshing
by ranjithmadawala (0 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
I am not discouraging you, but I just dont see a need for an article on this issue. One can find out those errors on trial and error basis with a test program. Give us something new and creative!
This thread is for discussions of Program Entry point in C# .