-6

I am new with C# I got really stuck on this problem.

The task is to Write a C# program that implements bubble sort. The program gets the values as command-line arguments.

Here is my code:

namespace BubbleSort
{
    class MySort
    {
        public void Main(int[] args)
        {
            int temp;
            for (int j = 0; j <= args.Length - 2; j++)
            {
                for (int i = 0; i <= args.Length - 2; i++)
                {
                    if (args[i] > args[i + 1])
                    {
                        temp = args[i + 1];
                        args[i + 1] = args[i];
                        args[i] = temp;
                    }
                }
            }
            Console.WriteLine("Sorted:");
            foreach (int p in args)
                Console.Write(p + " ");
            Console.Read();
        }
    }
}

And it always gives me error CS5001 Thank you in advance.

StackZ
  • 1
  • 5
    Did you look up CS5001? What did the error say? Did you read [the documentation](https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs5001)? – mason May 18 '21 at 19:08
  • 2
    *read* the error message and you will see it has nothing whatsoever to do with bubblesort, arrays. commandlines or arguments. In fact the text of the real issue is highly suggestive of the solution. – Ňɏssa Pøngjǣrdenlarp May 18 '21 at 19:13
  • `-->static<-- public void Main(int[] args)`. Also you need to check the Length of args before looping on -2 from 0 else index will be [out of range/bounds](https://stackoverflow.com/questions/24812679/what-is-an-index-out-of-range-exception-and-how-do-i-fix-it)... –  May 18 '21 at 19:16

1 Answers1

1

Hello for me the problem is the declaration of the main function. It should be:

public static void Main(string[] args)

You shoud cast your args in int: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number

Don't forget the static key word, without it the function need a target.

Dinasty
  • 11
  • 1