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

MVC - Introduction

What is MVC? Model-View-Controller (MVC) has been an important architectural pattern in computer science for many years.It was originally named Thing-Model-View-Editor in 1979, it was later simplifi ed to Model-View-Controller.Although originally developed for desktop computing, model–view–controller has been widely adopted as an architecture for World Wide Web applications in major programming languages. Difference between MVC, MVP and MVVM MVC The input is directed at the Controller first, not the view. That input might be coming from a user interacting with a page, but it could also be from simply entering a specific url into a browser. In either case, its a Controller that is interfaced with to kick off some functionality. There is a many-to-one relationship between the Controller and the View. That’s because a single controller may select different views to be rendered based on the operation being executed. Note the one way arrow from Controller to View. This ...

C# - DataTypes

                                                                                                      <<PreviousPage      NextPage>> Data Types Value Type  Variable contains value directly  Derived implicitly from the System.ValueType Cannot derive a new type from a value type. example of value type - int, float, enum. Builtin data types apart from the above mentioned built in data types we need to discuss two special type of data types - Struct and Enum Struct -  A struct type is a value type that is typically used to encapsulate small groups of related variables In the below example we are using a struct variable Book which has one decimal value price an...

C - Overview

What is C? It is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It is designed and written by Dennis Ritchie. C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C. The evolution of C took place in the following way: Why should we use C? 1.C language is a building block for many other currently known languages. 2.There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs. 3.C language is a structured programming language. This makes user to think of a problem in terms of function modules or blocks. Collection of these modules makes a complete program. This modular structure makes program debugging, testing and maintenance easier. What is ANSI C? 1.ANSI C, ISO...