why that method was made static?

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
Hello all,

im new here. Is this where beginners can ask stupid c# questions? or is there another section somewhere?

i will post now and im sure it can get moved to the relevant location if this isnt it.

anyway following a app tutorial on youtube, loving it.

but in the code:

to take this error away, he has to make the ChooseAction method static. Why does this have to be done? I thought static was only reserved for specific requirements. I understand what static is, it makes the variable or method available to the entire program right?
but i dont understand why that method was made static. Sorry very new to all this. please help if you can.

C#:
int action = ChooseAction();

lastly, are method names started with a capital like OpenDoor
and are variables lower like frontDoor?

cause i see a few inconsistencies.




C#:
using CarClassLibrary;
using System;

namespace CarShopConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the car store. First you must create some car inventory. Then you may add some cars to the shopping cart. Finally you can checkout which will give you the value of the shopping cart.");

            int action = ChooseAction();

            while (action != 0)
            {

            }

           


            Console.ReadLine();
        }

        public static int ChooseAction ()
        {
            int choice = 0;
            Console.WriteLine("Choose an action: \n(0) to quit \n(1) to add to inventory (2) to add a car to cart (3) to checkout");
            choice = int.Parse(Console.ReadLine());
            return choice;
        }
    }
}
 
The Main method, which is the entry point for your app, is static and so it can only call other static methods. A method or other member can either be static or it can be an instance member, where each instance of that type has its own instance of the member. When an instance method calls another instance method, it calls the method on the same instance. In a static method there is no current instance - possibly no instances have been created at all - so how could it call an instance method?
 
lastly, are method names started with a capital like OpenDoor
and are variables lower like frontDoor?
This is a naming convention recommended by Microsoft when writing code for the .NET Framework. Is so that people will readily be able to distinguish methods from variables from propeties.

 
Back
Top Bottom