Wednesday, 19 September 2012

Starting With C# Fundamentals - 1

Compilers

Lets know from you only what is Compiler ? Do you have some idea about it ? Anything you have heard or have some knowledge about it ? if Yes , Then its good and 
If no, then not to worry i am here to tell YOU....

From the help of example i will explain you :

Think that if you hold a page of delicious dish recipe but it is written in other language which not understandable by you.... what will you do ? 
Then you will have to Translate that other Language with the help of Translator into the Language understandable by you(English or any other)..... 

Similarly Codes written in different programming language ,first have to be converted into Machine Language understandable by computer which is translated by Compiler.

Compiler is a special program and also follows I-P-O(Input-Process-Output) Cycle . It takes the Code from the user as input and then the code is processed and gets converted in machine Language and then that coding is executed by the compiler.

Each programming languages has its own compiler. for ex- JAVA has Java compiler , C# has C# compiler.


Namespaces

Namespaces acts as container which contains group of assemblies , classes.For ex- Folder(namespaces) contains File(classes,assemblies).

Namespaces are the way in which C# extracts the .NET library classes.

using System;// System is a namespace
class sample
{
public static void Main()
{

Console.WriteLine("Hello");
}
}


In this example above, System is the Namespace where Console class is located.

  • A class in a namespace can be accessed by using the Dot(.) operator in C#. For Ex-
    • WriteLine Method is accessed by using the DOT(.) operator.
  • Here Console is a class and the WriteLine Is the Method.
  • using directive is used to import the namespace System into the program.
IN THE ABOVE EXAMPLE:-

When The compiler will parse the Console.WriteLine method , compiler will understand that the method is undefined. Then it will search for the namespaces in the program which has using directive and upon finding it will compile the code without any Error. 



Some Tips:

1. C# is a case sensitive Language.
  • using directive will written as shown above.
  • System will also be written as same as above.
  • Console.WriteLine will be also written as above.
  • class will be also be written as above.
  • Main,public will also be written as above.


2. You can add comment by using // as above anywhere in the           program.



Variables

So, Do you have any idea about what is a variable ? 

Not to worry I am here to tell you Friends.....

A variable is an identifier that denotes a storage location used to store a data value.

While creating a program you might need to store values , for that you need variables.

For example-You are creating a program to add two numbers and display the sum as a result.... for that you have temporarily store the numbers in the variables and then add these numbers.

Basically, Variable is a location that has name and is temporarily used to store the values. The Value can be Integer, characters or a Decimal value.



A variable name must be declared by specifying the data type as prefix.




Data Type defines the type of data that can be stored in a variable.


For example- A variable Contact NO. will ideally store the Integer.

C# provides various Built-In Data types. Built-In Data types are predefined data types that can be directly used in the programs to declare the variables.



-> C# also provides user-defined data type such as classes(explained before), structures , Arrays, Delegates and Interfaces(will study in subsequent chapters).



Types of Data Type

1.Value type
2.Reference Type

Value Type directly contains the value.
Char , int and float are the examples of the Value Type Variables.
For ex- When You declare an char variable ,system allocates the memory to store the character value in it.

Reference type-The Reference type variables contains the reference or address of the data stored in the memory.


Declaration Of Variables:-

int count;
char c,d,e,f;
double pi;

-> Variables are separated by commas means you can declare as     
   many variables with same data type by separating with commas   
   and ending with semicolon. For ex- 
   

   int addition,value,minus,multiply......,himanshu;

Initialization of Variables

A variable must be given value after it has been declared but before it is used in the expression.

for ex-

int x;
x=100;

    OR
int x=100;

char cc='X';    // If you will use a character data type , then    
               //  while inserting value, you have use single 
              //   quotes as shown in the example.




string str;
str="Anything";

OR                        // if you will use a string data type,  
                         //  then while inserting value , you                      
                        //   have use double quotes as shown in  
                       //    example

string str="Anything"; 


Constant Variables

The variables whose value does not change during the execution of the program are know as Constants.
For ex- Number of student in a class in a program may not change during the execution of the program.

const keyword is used to make the variable Constant.



example-
const int Rows=10;
const int Num=11;



const int Peace; // is illegal.
Peace=1;            //  By definition, a constant cannot have 
                      //   its value changed later.
        
Advantages
1.They make our program easy to understand.
2.They make our program easy to modify.

Note:
After declaration of constants, they should not be assigned any other values.



.........................To be Continued

Thank You 
Himanshu Chawla
himanshuchawla4393@gmail.com








No comments:

Post a Comment