C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






The Main() Method


Posted Date: 15 Aug 2008    Resource Type: Articles    Category: General

Posted By: Gaurav Arora       Member Level: Gold
Rating:     Points: 10



Scope: The scope of this article is to its practical scenario.

The Main() method is the execution method of C# programs, as you did in earlier programs. This method must be a static and has optional public modifier, but if you have write private as a modifier, program still execute this method. The Main() method must have return type either int or void.

/* This Example is a part of different 
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : gaurav.aroraose@yahoo.co.in*/

// File name : mainmethod.cs

using System;

namespace CSharp.AStepAhead.mainmethod
{
class mainmethod
{
static int Square(int num)
{
return (num * num);
}
public static int Main()
{
Console.WriteLine("This is Main() method from where your program execution done\n");
Console.WriteLine("Enter number: ");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Square of {0} is {1}", n, Square(n));
Console.ReadLine();
return 0;
}
}
}


Does program has Multiple Main() Method(s)?


In one instance yes, a program has multiple Main() method. Now, question arises Main() method is an entry point of a code where program execution done, then how its possible. You can define multiple Main() method(s) in a program but, when compiling the same program then you have to explicitly describe to the compiler which one Main() method has the entry point. It will clear from the following example:

/* This Example is a part of different 
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : gaurav.aroraose@yahoo.co.in*/

// File name : multimainmethod.cs

using System;

namespace CSharp.AStepAhead.multimainmethod
{
class anotherMain
{
public static int Main()
{
multimainmethod.Main();
return 0;
}
}
class multimainmethod
{
static int Square(int num)
{
return (num * num);
}
public static int Main()
{
Console.WriteLine("This is Main() method from where your program execution done\n");
Console.WriteLine("Enter number: ");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Square of {0} is {1}", n, Square(n));
Console.ReadLine();
return 0;
}
}
}


To overcome above error, you have to define the entry point of the program, using /Main switch , as follows:

Can we pass arguments to Main() Method?


Till now, you have used Main() method without parameter, but we can pass the arguments to Main() method. C# will accept any command-line arguments which is a string array like other language (C++). The arguments traditionally used as args, but in C# you can supply any name as per your choice.

As, the command-line arguments are in the form of string array, so, you can retrieve the same as you will retrieve the elements of any array.

The following bunch of code(s) describe the same:
/* This Example is a part of different 
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : gaurav.aroraose@yahoo.co.in*/

// File name : mainmethodargument.cs

using System;

namespace CSharp.AStepAhead.mainmethodargument
{
class mainmethodargument
{
public static int Main(string[] argument)
{
Console.WriteLine("This Main() method accepts Command-line arguments\n");
for (int i = 0; i <= argument.Length - 1; i++)
{
Console.WriteLine(argument[i]);
}
Console.ReadLine();
return 0;
}
}
}







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Interviews  .  C# Syntax  .  Article  .  .net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Jump Statement(s),Enumerations, Arrays, ArrayList
Previous Resource: Compiling C# Files
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use