C# language Basic Program Structure: You Must Know.

23:49
In last article we have seen how to setup software and installation of framework. So now before we proceed we must know and capable to know which part called what. So here I am explaining various part of C# language basic program structure.


 
Every C# program must contain the following parts.

  • Namespace 
  • A class 
  • Class members 
  • Class attributes 
  • Class methods 
  • A Main method 
  • Statement And Expression 
  • Comments
Here we will see sample example later on we will see how to create sample program.
Let’s have look at sample example.

using System;
using System.io;
namespace MyFirstCSharpProgram
{
class FistApplication
{
static void Main(string[] args)
{
/*This is comment*/
String myname="Dharmesh";
Console.WriteLine("Hi, My Name is : "+myname);
Console.ReadKey();
}
}
}
In above code starting from the top first you can see is:

System Namespace:

using System;
using System.io;

Code start with prefix keyword using then it has to be a namespace. This are system namespaces. Namespaces are collection of classes. Once you create them then you can use their functionality. There are 2 types of namespaces 
  1. System namespace an 
  2. User defined namespace.
Namespaces are predefining classes that help the developers to assist and make their work easier and smoother. When you create program with Visual studio it will include many using statement initially when you create program. How to create program we will see it later chapter.

User Define Namespace:

After that you can see keyword namespace.

namespace MyFirstCSharpProgram

This is key feature to identify between system namespace and user namespace. System namespace as specified earlier start with using keyword prefix while user namespace is created by user and it start with prefix keyword namespace. Here MyFirstCSharpProgram is user define namespace.
 
Class:

As I have explained earlier namespace is collection of classes. So we can say that one namespace can have multiple classes. Classes are collection of variables and methods that define its data and behavior. One of the most important methods in programming is MAIN method. In OOPS languages main method defines entry point of each program. Entry point is nothing but from where program is starting its execution. It is easy to identify the class. It have syntax like:

Class classname
{
}

Here class is keyword and classname is class name that user provide. In above code

class FistApplication

here FirstApplication is class name.

Variable And Methods:

Classes contain variable and methods. There are 2 types of methods user created and system methods. 

In above code 

string name ;

Here string is datatype and name is actual variable name. Variable are nothing but space reserved by system for storing particular type of data in that reserved space. Here string means it can store alphanumeric data in that space and for our naming convention, since this variable is storing name of the person so I have given it name as myname.
In this example there is not user created methods here we are using system methods. We can user system classes and methods using period (.) operator provided proper namespace are imported. 

Here we have used this code:

Console.WriteLine("Hi, My Name is : "+myname);
Console.ReadKey();

Here Console is class and WriteLine() and Readkey() are methods. Methods in C# always end with open and closed round breakers. Some methods do not accept parameters where some may accept parameters. You will get better idea when we see more examples. Here WriteLine() method accepts parameter while Readkey() do not accept any parameter.

Comments:

Comments are the lines that are for programmer’s reference they are not a part of your code. They are just indicating something about that peace of code or special remark about that code. They will not get compile nor executed. It always a good practice to write in comment what code or method is doing or special remarks where some critical piece of code is written.
There are 2 types of comment in every programming language :

  1. Single Line Comment
    This comment are only limited to single line. Example:

    //This is single line comment
     
  2. Multi Line Comment
    In above code we have actually used multi line comment.

    /*This is multiline comment here

    This will continue till it encounter this */
/*    */ Anything written between this signs are treated as multi line comment by complier.

Rules That You Need To Follow:

Every programming have specific rules that programmer need to follow to write compiler error free programming. 

Here you have seen after end of every line there ; Since this indicate end of statement to the compiler. 

Period that work as separator for accessing class and methods.

{  } This is called code block that isolate scope of variable and accessibility. This we will see in detail in upcoming sessions.
Methods always have () round breakers as postfix after method name.
You cannot name variable same as reserved keywords like string class; this is invalid since class is keyword.

You must import system namespace properly in order to use their classes and methods.
If you are very new to programming you will be feeling like not getting anything. Just keep in mind these things. It will automatically started making sense once you learn little more. Still you have any query or any suggestions do comment below. Thanks for reading.

Share this

Related Posts

Previous
Next Post »