locked
static void Main(string[] args).........question

    Question

  • I am relatively new to c#. My experience so far has been with console apps. I have recently learned that the user input from the keyboard for a console app is of the datatype string. I am trying to figure out the significance of the word string in the following code. I am guessing that it is due to the fact that the keyboard input is a string datatype. I am also guessing that this is telling the compiler the type that will be passed is string.

     

    static void Main(string[] args)

     

    Can anyone confirm or correct me on this.....................................................thanks

     

    Wednesday, October 24, 2007 11:33 PM

Answers

  • Hi,

     

    If this exe is run from command prompt then All parameter seperated by space are passed as arguments to Main method

     

    Let your exe name is test.ext

     

    C:\> test.exe My Parameters Are This

     

    then args contain

    args[0] = My

    args[1]=Parameters

    args[2]=Are

    args[3]=This

     

    It is considered that all parameters passed to main is type of string only.

    Yes your reson is right.

     

    Regards,

    Manju Sandhu

    Thursday, October 25, 2007 4:53 AM
  • there are many kinds of Main

    static void Main()  // no arguments

    static void Main(string [] args)

    static int Main()
    {
    return 0;
    }



    also you can write more than two main methods in one project.
    but you must set only one entrypoint to let the complier know which one is the right entrypoint.
    Thursday, October 25, 2007 5:51 AM
  • Hi,

     new to sql server wrote:

    the keyboard for a console app is of the datatype string


    Not only for console app, for all values from keyboard will be string. Then we can cast it to any datatype of our choice. Main method can be overloaded. But it can be parametrless or it has to accept string array.

     new to sql server wrote:

    I am also guessing that this is telling the compiler the type that will be passed is string.


    YES
    Thursday, October 25, 2007 11:47 AM

All replies

  • Hi,

     

    If this exe is run from command prompt then All parameter seperated by space are passed as arguments to Main method

     

    Let your exe name is test.ext

     

    C:\> test.exe My Parameters Are This

     

    then args contain

    args[0] = My

    args[1]=Parameters

    args[2]=Are

    args[3]=This

     

    It is considered that all parameters passed to main is type of string only.

    Yes your reson is right.

     

    Regards,

    Manju Sandhu

    Thursday, October 25, 2007 4:53 AM
  • there are many kinds of Main

    static void Main()  // no arguments

    static void Main(string [] args)

    static int Main()
    {
    return 0;
    }



    also you can write more than two main methods in one project.
    but you must set only one entrypoint to let the complier know which one is the right entrypoint.
    Thursday, October 25, 2007 5:51 AM
  • Hi,

     new to sql server wrote:

    the keyboard for a console app is of the datatype string


    Not only for console app, for all values from keyboard will be string. Then we can cast it to any datatype of our choice. Main method can be overloaded. But it can be parametrless or it has to accept string array.

     new to sql server wrote:

    I am also guessing that this is telling the compiler the type that will be passed is string.


    YES
    Thursday, October 25, 2007 11:47 AM
  • Hi

     

    It is also possible to use the arguments at any other location outside the main without remember the arguments in a own variable:

    Code Block

    static void Main(string[] args)

    {

    Console.WriteLine("Main");

    foreach (string arg in args)

    {

    Console.WriteLine(arg);

    }

    OtherLocation();

    }

    static void OtherLocation()

    {

    Console.WriteLine("Other Location:");

    // ATTENTION: The first argument is your application location

    foreach (string arg in System.Environment.GetCommandLineArgs())

    {

    Console.WriteLine(arg);

    }

    }

     

     

    Regards

    Flo

    Thursday, October 25, 2007 12:09 PM
  • Hello!

    What does this mean?: static void Main(string[] args)

    The code comes from this code:
    The program is convert strings to Morse-code.
    One thing is missing and that is the declaration of: string[]; // tabel with morse-codes (is that right)

    string morse[] = {".-",    "-...",  "-.-.", "-..",  ".",    "..-.",
                   "--.",   "....",  "..",   ".---", "-.-",  ".-..",
                   "--",    "-.",    "---",  ".--.", "--.-", ".-.",
                   "...",   "-",    "..-",  "...-", ".--",  "-..-",
                   "-.--",  "--..",  ".--.-",".-.-", "---." };

    Is this declaration of the table right if you compare with the code?
    What does string[] means in the first line?
    static void Main(string[] args)
            {
                string morse = Läs("Det du vill omvandla till morsekod: ");
                string alfa = "abcdefghijklmnopqrstuvwxyz";
                string salfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                for (int i = 0; i < morse.Length; i++)
                {
                    if (alfa.IndexOf(morse[i]) >= 0)
                    {
                        Console.Write(kod[alfa.IndexOf(morse[i]) + 1] + " ");
                    }
                    else if (salfa.IndexOf(morse[i]) >= 0)
                    {
                        Console.Write(kod[salfa.IndexOf(morse[i]) + 1] + " ");
                    }
                    else if (morse[i] == ' ')
                    {
                        Console.Write(kod[0]+" ");
                    }
                    else
                        Console.Write("?" + " ");
                }
                Console.WriteLine();
            }

    /Puckosmucko
    /thanks in advance


    Saturday, January 30, 2010 5:30 PM
  • Dear,

    Code works fine :

    string[] kod ==> there is an array of strings

     

     

     

     

    static void Main(string[] args)
            {
               
                    string[] kod = {".-",    "-...",  "-.-.", "-..",  ".",    "..-.",
                   "--.",   "....",  "..",   ".---", "-.-",  ".-..",
                   "--",    "-.",    "---",  ".--.", "--.-", ".-.",
                   "...",   "-",    "..-",  "...-", ".--",  "-..-",
                   "-.--",  "--..",  ".--.-",".-.-", "---." };
                    

                string morse = ("Det du vill omvandla till morsekod: ");
                string alfa = "abcdefghijklmnopqrstuvwxyz";
                string salfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                for (int i = 0; i < morse.Length; i++)
                {
                    if (alfa.IndexOf(morse[i]) >= 0)
                    {
                        Console.Write(kod[alfa.IndexOf(morse[i]) + 1] + " ");
                    }
                    else if (salfa.IndexOf(morse[i]) >= 0)
                    {
                        Console.Write(kod[salfa.IndexOf(morse[i]) + 1] + " ");
                    }
                    else if (morse[i] == ' ')
                    {
                        Console.Write(kod[0]+" ");
                    }
                    else
                        Console.Write("?" + " ");
                }
                Console.WriteLine();
            }

    Tuesday, June 22, 2010 8:07 AM
  • new to VS 2012 I get

    Error    1    Program 'c:\Users\Landon\Documents\Visual Studio 2012\Projects\TCP_client\TCP_client\obj\Debug\TCP_client.exe' does not contain a static 'Main' method suitable for an entry point    C:\Users\Landon\Documents\Visual Studio 2012\Projects\TCP_client\TCP_client\CSC    TCP_client

    when I build the following:

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

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

    Friday, September 21, 2012 4:18 AM