BBirdsell
BBirdsell

Reputation: 207

Problem outputting result to command line.

isALeapYear[x_Integer]:= 
If[(Mod[x,4]==0 && !Mod[x,100]==0) || (Mod[x,4]==0 && Mod[x,400]==0),ToString[x] <>" is a leap year", ToString[x] <> " is NOT a leap year"]

Below is my attempt to solve the exercise "Leap" from Exercism.com as I struggle to teach myself C#. The site suggests coming here for help. I have already figured out the logic in Mathematica (above) but my little C# program does not return anything when I run dotnet run in terminal. Any suggestions are appreciated.

using System;

public static class Leap
{
    public static bool IsLeapYear(int year)
    {
        if (year % 4 == 0 && year % 100 == 0) 
        {
            return true;    
        }
        else
        {
            return false;
        }
    }

    public static void main()
    {
        int yearq = 2015;
        bool result = IsLeapYear(yearq);
        Console.WriteLine(result);
    }
}

Upvotes: 0

Views: 72

Answers (2)

BBirdsell
BBirdsell

Reputation: 207

Updating the question for future prosperity, interestingly I solved my own question by adding:

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>

...to the PropertyGroup of the .csproj file from this tutorial website. Reading the page, I can't say I completely understand why it fixed my problem; hence leaving the question unanswered in case someone can better explain the answer. (It definately had something to do with the -main complier options mentioned elsewhere in the answers.)

Upvotes: 0

Ulf Kristiansen
Ulf Kristiansen

Reputation: 1631

When you create a new console project in Visual Studio a file named Program.cs is added automatically with a static Main method. This is the entry point of your application. The first line in the method is the line that will execute first.

It looks like this:

namespace SomeNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
             // By default this line will execute first
        }
    }
}

Microsoft Docs explains it better:

The Main method is the entry point of a C# application. (Libraries and services do not require a Main method as an entry point.) When the application is started, the Main method is the first method that is invoked.

There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.

I'm guessing you did't change Program.Main and the program just executes and does nothing.

I suggest moving the content of your main method into Program.Main.

static void Main(string[] args)
{
    int yearq = 2015;
    bool result = Leap.IsLeapYear(yearq);
    Console.WriteLine(result);
}

If however you do need to specify more than one class with a Main method you must tell the compiler which method to use as the entry point by using the -main compiler option.

You also have the option of deleting Program.cs and renaming your main to Main, but unless you have some special need to do so I would rather go with the default behavior.

Upvotes: 1

Related Questions