Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am currently learning C#. I want to run the following method, but I don't know what I need to put in the main method in order for it to build successfully:

C#
namespace ConsoleApp1
    {
        using System;
        using System.Linq;
        public class Program
        {
            public int[] SwitchLights(int[] a)
            {
                var count = a.Count(t => t == 1);
                for (int i = 0; i < a.Length; i++)
                {
                    var current = count;
                    if (a[i] == 1) count--;
                    if (current % 2 == 1) a[i] = a[i] == 1 ? 0 : 1;
                }
                return a;
            }
        }
    }


What I have tried:

I tried several things but haven't succeeded. Any help will be appreciated.
Posted
Updated 26-Oct-20 11:03am

Your program must include a main method in the class as follows:
C#
static void Main(string[] args)
{
    // add code here to call other methods of the class.
}
 
Share this answer
 
Comments
Member 14975654 26-Oct-20 12:47pm    
Thank you, I thought of that, but I was actually wondering what to code to put there to call it.
Richard MacCutchan 26-Oct-20 12:50pm    
The main method gets called automatically by the framework when you run the program. That is why it must be coded exactly as shown.
Richard Deeming 27-Oct-20 11:14am    
"That is why it must be coded exactly as shown."

With some variations accepted - eg:
* static void Main()
* static void Main(string[] args)
* static int Main()
* static int Main(string[] args)

And, since C# 7.1:
* static Task Main()
* static Task Main(string[] args)
* static Task<int> Main()
* static Task<int> Main(string[] args)
Richard MacCutchan 27-Oct-20 11:22am    
Yes, but that just serves to confuse beginners. Let's get them to understand the basics first.
Quote:
Thank you, I thought of that, but I was actually wondering what to code to put there to call it.

As Richard has said, the static Main method is called automatically for you - that's why an assembly can only ever have one static method called Main, regardless of how many classes it contains.

Once you have that, you can call your method(s) as necessary:
C#
using System;
using System.Linq;

namespace ConsoleApp1
    {
        public class Program
        {
            public int[] SwitchLights(int[] a)
            {
                var count = a.Count(t => t == 1);
                for (int i = 0; i < a.Length; i++)
                {
                    var current = count;
                    if (a[i] == 1) count--;
                    if (current % 2 == 1) a[i] = a[i] == 1 ? 0 : 1;
                }
                return a;
            }
            static void Main(string[] args)
            {
                int[] myArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
				Program myProg = new Program();
                myArray = myProg.SwitchLights(myArray);
            }
        }
    }
 
Share this answer
 
Once the static Main method runs, it will terminate immediately (the window will close) ... unless you use ReadKey, or ReadLine to cause the thread to stop.

A common technique is to run a 'while loop that keeps the process/window going until some exit condition is met.

You can see some techniques for Console programming here: [^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900