Yes, Main method of the program can be private.it does not effect on output of the program.
i have checked It practically using C# on the visual Studio 2010.
You can see it in Example..............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Factorial
{
public int fact(int n)
{
if (n == 0) return 1;
else return n * fact(n - 1);
}
private static void Main(string[] args)
{
Factorial ob = new Factorial();
Console.WriteLine("Type a number up to 20:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(ob.fact(n));
Console.Read();
}
}
}
And Second thing if any program contain more than one Main method then what will happen?
No Problem, if first main method declared as private and Second is Public then first Main method will compile and run but it can not be entry point of your Application.
For Example......
==> public static void Main(string[] args)
So it will not effect on the program output or in compilation and run. and take last Main method as the entry point of the Program...
for example............
--> private static void Main(string[] args)
i have checked It practically using C# on the visual Studio 2010.
You can see it in Example..............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Factorial
{
public int fact(int n)
{
if (n == 0) return 1;
else return n * fact(n - 1);
}
private static void Main(string[] args)
{
Factorial ob = new Factorial();
Console.WriteLine("Type a number up to 20:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(ob.fact(n));
Console.Read();
}
}
}
And Second thing if any program contain more than one Main method then what will happen?
No Problem, if first main method declared as private and Second is Public then first Main method will compile and run but it can not be entry point of your Application.
For Example......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MAinmethod
{
==> private static void Main(string[] args)
{
Console.WriteLine("Its awsome,, i am private and runnig here");
}
class testing
{
{
Console.WriteLine("i am public and running here");
MAinmethod.Main(args);
Console.Read();
}
}
}
}
And Next question arise that if given example both are private then what will happen?
So it will not effect on the program output or in compilation and run. and take last Main method as the entry point of the Program...
for example............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MAinmethod
{
--> private static void Main(string[] args)
{
Console.WriteLine("Its awsome,, i am private and runnig here");
}
class testing
{
{
Console.WriteLine("i am public and running here");
MAinmethod.Main(args);
Console.Read();
}
}
}
}
out put of this Program will be............
No comments:
Post a Comment