what is the use of static keyword and why we are writing only static word in main method["static void main [string[] args"]?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Jignesh TrivediRank #16 · 50.3K reputationPosted Nov 29, 2015, 7:22 PM EST
0
Main method is the necessary entry point for any "Executable" and it must be Static because that is required in order to access the method without having an instance of the object to address. In order to invoke the method from outside the application, a static method is required.
Raja TRank #685 · 3.5K reputationPosted Nov 29, 2015, 7:44 PM EST
0
Hi Gantasala,
The static keyword is used to specify a static member, which means static members are common to all the objects.
This keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
Finally i understand the static variable.....static variable shared the value of it among all instances of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
classProgram
{
staticint a; // initialized at the time of declaration
Jignesh TrivediRank #16 · 50.3K reputationPosted Nov 29, 2015, 7:22 PM EST
Ravi PatelRank #439 · 5.7K reputationPosted Nov 29, 2015, 7:42 PM EST
for more details check following link
Raja TRank #685 · 3.5K reputationPosted Nov 29, 2015, 7:44 PM EST
Finally i understand the static variable.....static variable shared the value of it among all instances of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static int a; // initialized at the time of declaration
public static int Hello()
{
a = 20;
return a;
}
}
class run
{
static void Main(string[] args)
{
//Program p = new Program();
Console.WriteLine(Program.Hello());
}
}
}
Output
20
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts