null
null

Reputation: 1092

Console closing instantly not going through the code

Just been learning C# methods on SOLO LEARN, i'm on method parameters the app have it's own compiler and totally different from visual studio so i can't get this code to work. When i try to run the console it closes instantly even tho i have Console.Read();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {


        }

        void myFunc(int x)
        {
            int result = x / 2;
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

Upvotes: 1

Views: 93

Answers (2)

Ervis Trupja
Ervis Trupja

Reputation: 2800

You are not executing the function you have created inside your main. In a console app static void Main(string[] args) is the function where application starts. So, in order to have your application started you need to execute your myFunc(int x) function inside it.

Make the changes like below:

static void Main(string[] args)
        {
           myFunc(4); //You need to put an integer here              
        }

Upvotes: 0

Roman
Roman

Reputation: 12171

You don't call anything from Main(). Call myFunc() from Main():

static void Main(string[] args)
{
    myFunc(4);
}

Also you should make myFunc() to be static:

static void myFunc(int x)

OR

You can create instance of Program and call this function without making myFunc() static (simply change your Main() method):

static void Main(string[] args)
{
    Program p = new Program();
    p.myFunc(4);
}

Upvotes: 2

Related Questions