Hie friends
Below is my sample code with no main method when tried to execute it's throwing an exception lol it will i too know that
But the thing in java[JVM] many says after executing static blocks the after it looks for main if no main then after it throws an exception ?? right .. IF happens so as java , c# both are related OOPL's where in c# why am i not getting o/p before exception were as i am getting o/p i.e hiee from sunil in case of java under JVM ??
class Sunil
{
static void sayhello()
{
System.Console.WriteLine("hie from sunil");
}
}
Hope you got my point ?
THANK YOU
Loading

VulpesPosted May 24, 2013, 5:05 AM
This is guaranteed to run before any static method or instance constructor of the containing class is executed and runs (at most) once. In the case of the class which contains the Main() method, it even runs before the Main() method itself is called by the system.
Here's your class, Sunil, with the sayhello() method changed to a static constructor:
When this is compiled and run, the output is:
Notice also the syntax of the static contructor which always has the same name as the containing class, takes no parameters and has no access modifier.
VulpesPosted May 24, 2013, 10:17 AM
Not surprisingly, the designers of the two languages had different views on the way certain things should be implemented.
If C# had done everything the same as Java, it would have been accused of being a 'rip-off' of the latter - in fact, it was accused of this anyway :)
But, what matters is that you have a mechanism for initializing the static fields of a class, even if the syntax is different.
SUNIL GUTTAPosted May 24, 2013, 9:19 AM
The equivalent of a static block in Java is a static constructor in C#. Nice point TY
Humm yes i know that in-case of c# about static constructor but i am having doubt like y its going like that in java ?? I mean both are OOPL's y the differences ..
is there problem with compiler oorrr ?? what
SUNIL GUTTAPosted May 24, 2013, 4:33 AM
@riddi :: after making the method, where have you called it?? .!!
As i declared my method under STATIC block i haven't called i know that its a rule to call methods but the thing i am trying to point out here is the same code when we write in java and compile it we get out put then after we get exception but in c# we getting direct exception ??
As both are OOPL's whats the difference ? Main should be entry point for both .. does starting point decided by compiler ???
Hope i made myself clear
Pankaj Pandey : yes but once plz look above that will help to answer to my point
Thank you
Pankaj PandeyPosted May 24, 2013, 2:05 AM
Opps concept are same but coding style are diffrent in both languages.
You need to use Main function coz it is controller of programm just like our brain.
if you remove it, you will get error or exception
Riddhi ValechaPosted May 24, 2013, 1:01 AM
Please explain the question in details....
And... after making the method, where have you called it??