Visual Studio Developer Center > Visual C# Forums > Visual C# General > Very Basic Question - Just Learning

Answered Very Basic Question - Just Learning

  • Sunday, February 12, 2012 4:18 AM
     
     

    I am running the below code that I have built on from the Hello World! application.  I have been able to compile it and run it from command prompt and from within Visual Studio for debugging.  the strange thing is that even if I do not put any args after the command line it still does not process the if statement.  I added the "Console.WriteLine(args.Length);" line in there so I could prove to myself that it was in fact null but the program keeps skipping over the iff statement.  Also, if I do put an arg after the command line it processes the code "Hello, System.String[]!"...  I know this is very basic so thanks for the help!

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


    namespace HelloWorldVS
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(args.Length);
                if (args == null)
                {
                    Console.WriteLine("Hello, World!");
                    Console.ReadLine();
                }
                else
                {
                    Console.Write("Hello, ");
                    Console.Write(args);
                    Console.WriteLine("!");
                }
                Console.ReadLine();
            }
        }
    }

Answers

  • Sunday, February 12, 2012 4:35 AM
     
     Answered Has Code

    args will never be null, it will just be an array of 0 strings.  I have corrected the code below.  Additionally, you didn't need the Console.ReadLine(); when there are no arguments, so I commented it out.  Also, I didn't think that when you were printing your arguments that it was giving you quite what you expected, so I corrected that as well.

    Hope this helps.

      static void Main(string[] args)
      {
       //Console.WriteLine(args.Length);
       if (args.Length == 0)
       {
        Console.WriteLine("Hello, World!");
        //Console.ReadLine();
       }
       else
       {
        Console.Write("Hello, ");
        foreach (string arg in args)
        {
         Console.Write(arg + " ");
        }
        Console.WriteLine("!");
       }
       Console.ReadLine();
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed As Answer by AdaveshMVP Sunday, February 12, 2012 4:40 AM
    • Marked As Answer by tlitterio Sunday, February 12, 2012 2:25 PM
    •  
  • Sunday, February 12, 2012 4:38 AM
     
     Answered

    Try:

    if(args.Length ==0)

    Args array is not null it is just empty.

    Regards

    Jack Lichwa


    Jack Ernest Care About Your Craft

    • Marked As Answer by tlitterio Sunday, February 12, 2012 2:25 PM
    •  

All Replies

  • Sunday, February 12, 2012 4:35 AM
     
     Answered Has Code

    args will never be null, it will just be an array of 0 strings.  I have corrected the code below.  Additionally, you didn't need the Console.ReadLine(); when there are no arguments, so I commented it out.  Also, I didn't think that when you were printing your arguments that it was giving you quite what you expected, so I corrected that as well.

    Hope this helps.

      static void Main(string[] args)
      {
       //Console.WriteLine(args.Length);
       if (args.Length == 0)
       {
        Console.WriteLine("Hello, World!");
        //Console.ReadLine();
       }
       else
       {
        Console.Write("Hello, ");
        foreach (string arg in args)
        {
         Console.Write(arg + " ");
        }
        Console.WriteLine("!");
       }
       Console.ReadLine();
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed As Answer by AdaveshMVP Sunday, February 12, 2012 4:40 AM
    • Marked As Answer by tlitterio Sunday, February 12, 2012 2:25 PM
    •  
  • Sunday, February 12, 2012 4:38 AM
     
     Answered

    Try:

    if(args.Length ==0)

    Args array is not null it is just empty.

    Regards

    Jack Lichwa


    Jack Ernest Care About Your Craft

    • Marked As Answer by tlitterio Sunday, February 12, 2012 2:25 PM
    •  
  • Sunday, February 12, 2012 2:24 PM
     
     
    Ah, OK, I figured it was something that had to deal with the args not being empty.  Thanks!
  • Sunday, February 12, 2012 2:25 PM
     
     
    Thanks!