Question about main function
I have often seen code something similar to
class Program
{
static void Main(string[] Args)
{
(new Program()).run();
}
public void run();
{
//code generally seen in main function;
}
}
Can anyone tell me what is the purpose for following such a method of coding and what are the advantages disadvantages for the same?
Re: Question about main function
Re: Question about main function
It is defined standard which you must obey if you want you application to run.
Quote:
Originally Posted by MSDN
The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.
Re: Question about main function
Quote:
Originally Posted by
boudino
It is defined standard which you must obey if you want you application to run.
You know, I thought the same thing at first, but I think that the OP is more concerned with the code inside of main, i.e., (new Program()).run(). I have never seen anyone do that before...
Re: Question about main function
the code is in C#
you can find similar code at http://www.java2s.com/Code/CSharp/De...romconsole.htm
here is a sample application for reading input from 2 numbers posted at above site
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
(new Program()).run();
}
public void run() {
double dailyRate = readDouble("Enter your daily rate: ");
int noOfDays = readInt("Enter the number of days: ");
writeFee(calculateFee(dailyRate, noOfDays));
}
private void writeFee(double p) {
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
private double calculateFee(double dailyRate, int noOfDays) {
return dailyRate * noOfDays;
}
private int readInt(string p) {
Console.Write(p);
string line = Console.ReadLine();
return int.Parse(line);
}
private double readDouble(string p) {
Console.Write(p);
string line = Console.ReadLine();
return double.Parse(line);
}
}
Re: Question about main function
I'm using similar code quite often, even I preferre to keep Program class just with Main() function and create a new class for the application logick.
Re: Question about main function
Thanks Boudino,
I read some where that such a method keeps code safe. So in that case, my main function will contain a statement
(new NewClass()).run()
or should I be using
NewClass tempnewclass = new NewClass();
Are there any requirements that my new class should be static or of any other type....?
-harsh
Re: Question about main function