Jump to content




Recent Status Updates

  • Photo
      15 Nov
    duzamucha

    Hi, I am final year Interior Design Student from University of Huddersfield. I am currently working on my final major project which is going to be linked to coding. I was hoping that you could help me with my research. I have prepared a short survey, it would be a massive help if you could fill it in for me. It takes less than 2 minutes to complete, I promise. Here is the link: https://www.surveymonkey.com/s/73XLJKK Thank you so much in advance!

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

What does "static" void Main(string[] args) in C# mean?

string

  • Please log in to reply
14 replies to this topic

#1 gautham

gautham

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 11 January 2012 - 01:54 AM

When i googled it i found the following:


And Static Keyword shows that there should be something in the function which is static, i.e. WriteLine() is a static member method of the Console class.
==


I am unable to understand what does the above sentence mean, as I am beginner in OOPS and CSharp, i hope i can find some detailed answers along with some examples... please....

What are Public, Private, Protected and Static? can we use these keywords for both "Classes and Methods"?

How they are helpful?

thank you.
  • 0

#2 Rakso

Rakso

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 329 posts
  • Programming Language:Java
  • Learning:Java, C++

Posted 11 January 2012 - 05:55 AM

Here is explanation of them all:

Access modifiers

public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private
The type or member can only be accessed by code in the same class or struct.

protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

Static
The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

Theese keywords have the same effect on methods as they have on classes.

Edited by TheCompBoy, 11 January 2012 - 07:26 AM.

  • 0
Was my post useful? Please take your time and press the Like This button below my post, Big Thanks!

#3 mebob

mebob

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 11 January 2012 - 08:12 PM

"static" means what TheCompBoy quoted and that it can be executed without an instance of the class it resides in.
  • 0
Latinamne loqueris?

#4 mahmoud

mahmoud

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 12 January 2012 - 04:46 AM

void: mean that no return value is result from method like showing or printing some thing

Main: to refer to the entry point of your program to be compiled before executed when you click debug

(): the input of method

string: : the type of your array return with string so you need to take it in your brain when you deal with variable
[]: refer to an array

args: the name of array which is input of method that return array of string ( brief of arguments)

static:all member and variable can be accessed
Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.
  • 0

#5 gautham

gautham

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 12 January 2012 - 01:08 PM

namespace ConsoleApplication11
{
    class Program
    {
      private void Main(string[] args)
        {
            
            Console.WriteLine("Welcome");
        }
    }
}
====

When i am running the above program using “private” Access modifier then it displayed the following error message:


ConsoleApplication11.exe' does not contain a static 'Main' method suitable for an entry point.


thank you.
  • 0

#6 mebob

mebob

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 12 January 2012 - 01:36 PM

I think it requires Main to be static so it can be accessed without instantiating the class.
  • 0
Latinamne loqueris?

#7 gautham

gautham

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 13 January 2012 - 03:39 AM

@mebob:

I think it requires Main to be static so it can be accessed without instantiating the class.
==

How can we access Main without instantiating can you or anyone post an example of that.


thank you.
  • 0

#8 reed

reed

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 13 January 2012 - 06:10 PM

Main will run automatically when the program is started. However, if you wanted to call it again from another class:
class AnotherClass
{
     public void SomeMethod()
     {
         Program.Main(); //Calls Main without instantiating the 'Program' class.
     }
}

Edit:

Main has to be 'public' also. The 'private' designation keeps it hidden from methods in other classes; it can only be called by another method in the SAME class.
  • 0

#9 gautham

gautham

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 13 January 2012 - 10:33 PM

@reed: you said:

Main has to be 'public' also. The 'private' designation keeps it hidden from methods in other classes; it can only be called by another method in the SAME class.
==

When i am using "public" then it is still displaying the following message:

ConsoleApplication11.exe' does not contain a static 'Main' method suitable for an entry point.
==

my question is why "main()" should be "static" only?

  • 0

#10 reed

reed

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 14 January 2012 - 10:04 AM

@reed: you said:

Main has to be 'public' also. The 'private' designation keeps it hidden from methods in other classes; it can only be called by another method in the SAME class.
==

When i am using "public" then it is still displaying the following message:

ConsoleApplication11.exe' does not contain a static 'Main' method suitable for an entry point.
==

my question is why "main()" should be "static" only?


The short answer: That's just the way it is. .Net does not create an instance of the Program class when you execute the application.


Here is an example that might help:
class Vehicle
{    
    public Vehicle()
    {
    }

    public void SomeMethod() //This method cannot be accessed without creating an instance of Vehicle
    {
        //Do Something
    }
    
    public static void SomeOtherMethod() //This method can be accessed without creating an instance of Vehicle because it is marked 'static'
    {
        //Do Something Else
    }
}

class Program
{
    static void Main() //This method can be accessed without creating an instance of Program
    {
        Vehicle.SomeMethod(); //Error
        Vehicle.SomeOtherMethod(); //No error because the method is static
        Vehicle Car = new Vehicle(); //Creating an instance of the Vehicle class (Instantiating)
        Car.SomeMethod(); //No error because Car is an instance of Vehicle
        Car.SomeOtherMethod(); //No error.  This method can be used with or without an instance of Vehicle
    }
}

Main can also be 'public static', but never 'private static' or just 'private'. Private hides the method from other classes, .Net, and everything else that is not inside that class. Public makes doubly sure that the method is visible.
  • 1

#11 gautham

gautham

    CC Regular

  • Member
  • PipPipPip
  • 33 posts

Posted 15 January 2012 - 01:48 PM

@reed:

Thank you.

i have few more doubts, you might clarify...


namespace ConsoleApplication15
{
    class Employee2
    {
        private string name = "Gautham, Reddy";
        private double salary = 100.0;

        public string GetName()
        {
            return name;
        }

        public double Salary
        {
            get { return salary; }
        }
    }

    class PrivateTest
    {
        static void Main()
        {
            Employee2 e = new Employee2();

            // The data members are inaccessible (private), so
            // they can't be accessed like this:
            //    string n = e.name;
            //    double s = e.salary;

            // 'name' is indirectly accessed via method:
            string n = e.GetName();

            Console.WriteLine(n);

            // 'salary' is indirectly accessed via property
            double s = e.Salary;
                        
        }
    }
}


Now i understood how "Private" access modifier works but my question is "what is the use of "Private" access modifier in 'real time' " can you post an example?



thank you.
  • 0

#12 reed

reed

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 15 January 2012 - 07:19 PM

@reed:

Thank you.

i have few more doubts, you might clarify...


namespace ConsoleApplication15
{
    class Employee2
    {
        private string name = "Gautham, Reddy";
        private double salary = 100.0;

        public string GetName()
        {
            return name;
        }

        public double Salary
        {
            get { return salary; }
        }
    }

    class PrivateTest
    {
        static void Main()
        {
            Employee2 e = new Employee2();

            // The data members are inaccessible (private), so
            // they can't be accessed like this:
            //    string n = e.name;
            //    double s = e.salary;

            // 'name' is indirectly accessed via method:
            string n = e.GetName();

            Console.WriteLine(n);

            // 'salary' is indirectly accessed via property
            double s = e.Salary;
                        
        }
    }
}


Now i understood how "Private" access modifier works but my question is "what is the use of "Private" access modifier in 'real time' " can you post an example?



thank you.


The code you've posted actually does a pretty good job. Basically, you are locking the data or method down so that you have more control over how and when the data gets modified. It is simply good practice to keep all data private, along with methods that you only plan on calling from within the class.
  • 0





Also tagged with one or more of these keywords: string

Powered by binpress