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






Popular posts from this blog

C# - Decision Making

  <<PreviousPage     NextPage>> Decision Making If Else Statement If Else statement is used for decision making in c# . In the below example we will see how we can use if else statement in C# to great effect.   Nested If Else Statement We can improve upon the above example by adding nest if else statement. Nested if else statement helps to add extra if else block with in an if or else block. Switch Statement In place where we need multiple if else statements to satisfy a criteria we can add switch statement instead of if else statement.  We can create an application which would answer our question and let us know about the Date and Time. This will be a small AI sort of idea which would recognize your command and perform accordingly.  Code -    Console.WriteLine("................Welcome to Mini AI................");            str...

C# - Data Type Conversion

  <<PreviousPage      NextPage>> Data Type Conversion  In our previous tutorial we have seen various types of data and how to use them. In this session we are going to look into how to convert these data type from one to another.  Implicit Conversions In Implicit conversion can be made when the value to be stored can fit into the variable without being truncated or rounded off. We can take example of int and long, float and double. we can declare a variable int and assign it to a long type which is an implicit conversion as the scope of long is bigger than that of int so there is no worry of loosing data.  Explicit Conversions However, if a conversion cannot be made without a risk of losing information, the compiler requires that you perform an explicit conversion, which is called a cast. A cast is a way of explicitly informing the compiler that you intend to make the conversion and that you are aware ...