Skip to main content

C# - Basic Syntax



static void Main(string[] args)

If we go back to our first code example -

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!!! C# coders....");
        }
    }
}

Here we are using couple of things that need explanation before we start coding. 
Inside the class Program we have method Main(). Main() is the entry point to the application. When we execute our code it beings by invoking the method Main().

The Main method contains three components along with its name static, void, string[] args.

static - Static signifies that we don't need to create an instance of the class in order to call the Main() method.

void - void means that the Main() method doesn't have any return type it self.

string[] args - string[] args represents the argument that we pass when we call the Main() method. 

In order to understand how string[] args works and how Main() method is the entry point of any application in C#, we need to write a code.




In the above code we are trying to print the argument that we are passing to the Main() method. But when we are the application it throws an error. 




Now the reason why it is throwing error because we are trying to print the value of the string array args where we have not passed any value and it is null hence the error.

In order print the value of the argument of Main method we have to use a different way to call the Main method. 

We need to run the application from command prompt and we have to provide the argument to the Main method. Before that we have to open the .exe file from the location where we have saved the application. In this case we have to open the directory HelloWorld as it the name we had given to our application.



 After going to the directory we have goto \bin\Debug\ and open command prompt. 
now try to run the exe file HelloWorld.exe and in the command pass HELLO!!. Now it will print the value of HELLO!! with out any error. 



In the above example we have passed three arguments which shows three different results.

When we pass HELLO!!,  prints - HELLO!!
When we pass HELLO CODERS!!,  prints - HELLO
When we pass <space>HELLO!!,  prints - HELLO!!

So the above example shows that it only prints the string values before the first space because we are trying to print args[0] and all the spaces before the argument have been neglected.

Now we want to print all the values that we pass to the argument.

Lets change the code again and follow the same steps we have done for the above example






In the above example we have passed three arguments which shows three different results.

When we pass HELLO!!!,  prints - HELLO!!!
When we pass HELLO CODERS!!!,  prints - HELLO <new line> CODERS!!!
When we pass <space>HELLO<space>CODERS!!,  prints - HELLO <new line> CODERS!!!



So this example clears a lot of air regarding the args conundrum. We can see that since we are printing the whole string array args rather than just args[0], it now prints all the values after space. If we enter more values to pass it will print every thing that comes after a space with a newline (Console.WriteLine), meaning it will print args[0], args[1],args[2],args[3],args[3]......... values.



Using Statement



If we have take example of our first code, here we need explanation for the key word using.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!!! C# coders....");
        }
    }
}

using is used to import necessary packages that we need to run the application successfully. 


Commenting in C#


There are two ways we can add comments to a C# code

1.   using // 

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

            // find out the value of argument to Main() method
            foreach(string value in args)
            Console.WriteLine(value);
        }
    }
 with // we can comment only one line. 

2. To comment multiple lines we need to use / *  */
  
 class Program
    {
        static void Main(string[] args)
        {
                // find out the value of argument to Main() method
                  Console.WriteLine(args[0]);

                /* 

                            // find out the value of argument to Main() method
                            foreach(string value in args)
                            Console.WriteLine(value);
                */
        }
     }




How to declare and define a variable 


 static void Main(string[] args)
        {
            // variable declaration
            int id;
            string name="Coders"; // variable declaration and definition at the same time;
            double[] price = new double[10];
            // variable definition
            id = 10;
            price[0] = 45.34; 
        }






How to create an Object of a Class and call member methods