Monday, November 9, 2009

UNBOXING SAMPLE

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
int i = 5;
Object obj = i; // implicit boxing
int j = (int)obj;// explicit un-boxing

}
}
}

BOXING SAMPLE PROGRAM

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
int i = 10;
Object obj = i; // implicit boxing
obj.ToString();

}
}
}

BOXING AND UNBOXING

Boxing is implicit conversion of value type to oblect,
Unboxing is explicit conversion of object type to value type.

THE SEALED KEYWORD

This key word is used to prevent any class from inheriting. So if we want to fix a condition that no class should inherit from this class, then we can mention that class as sealed class.

public sealed class demo
{
public int a = 10;
public int b = 2;
public void add()
{
int c = a + b;
Console.WriteLine(c);
}
}
Now I cant inherit this class 'SINCE IT CONTAINS A KEY WORD SEALED'.

INHERITANCE IN C#.NET

I have already given the definition for inheritance. Here we can see a sample program for inheritance


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{
class MyClass
{
public int x, y;
public void readvalue()
{
x = 10;
y = 20;
}
public void printvalue()
{
Console.WriteLine("The Value of X= " + x);
Console.WriteLine("The value of Y= " + y);
}
}
class MainClass
{

public static void Main()
{
MyClass1 m = new MyClass1();
m.readvalue();
m.readnextvalue();
m.printvalue();
m.printnextvalue();
Console.ReadLine();
}
}
class MyClass1 : MyClass
{
public int p, q;
public void readnextvalue()
{
p = 30;
q = 40;

}
public void printnextvalue()
{
Console.WriteLine("The Value of P= " + p);
Console.WriteLine("The Value of Q= " + q);

}
}

}



This is the sample program to demonstrate inheritance.
The operator used for inheritance is ‘:’
the syntax is Class derived class: baseclass

In the above program,

class MyClass1 : MyClass

Shows that we are inheriting Myclass1 Form Myclass.

RUNTIME POLYMORPHYSM

Using virtual and override keywords in C#.net
Here the method name is same and the number as well as the type of the argument is the same. So, while executing which method is called first is to be determined at run time. For that we have to use two key words like virtual and override.
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{

class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing Shape...");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing Circle...");
}

static void Main()
{
Shape theShape = new Circle();
theShape.Draw();
}
}


}

METHOD OVERLOADING POLY MORPHISM (COMPILE TIME)

What is method over loading?

We can have more than one method in a class with a same name but different in number or type or the argument (parameters), this is called method overloading.

Let us see a sample program to explain this concept:

using System;

using System.Collections.Generic;

using System.Text;

namespace forblog

{

class demo

{

void add(int a, int b)

{

int c=a+b;

Console.WriteLine("The Sum is "+c);

}

void add(float x, float y)

{

float z=x+y;

Console.WriteLine("The Sum is "+z);

}

public static void Main(String []arg)

{

demo ob=new demo();

ob.add(10,20);

ob.add(23.4f,34.4f);

}

}

}

This is the sample program which contain same method name add but when you see the type of argument inside it, is different. We can also do it by making difference in the number of arguments. please try it your self.

And now …..Why it is called compile time polymorphism.

Do you know what is happening during compilation in program? During compilation the syntax of our program is checked. So, here in the above concept we are making difference in the syntax i.e. in first add method we have given two integers and in second add method, we have two float.

so this is an example for method overloading.

Sunday, November 8, 2009

METHOD WITH RETURN TYPE AND WITH ARGUMENT

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{

class obj
{
public int show(int a,int b)
{

int c = a + b;
return c;
}
public static void Main (String []args)
{
obj ob=new obj();
int d=ob.show(5,10);
Console.WriteLine("the sum is " + d);
}
}

}

METHOD WITH RETURN TYPE AND WITHOUT ARGUMENT

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{

class obj
{
public int show()
{
int a=5;
int b=10;
int c = a + b;
return c;
}
public static void Main (String []args)
{
obj ob=new obj();
int d=ob.show();
Console.WriteLine("the sum is " + d);
}
}

}

METHOD WITH OUT RETURN TYPE AND WITH ARGUMENT

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{

class obj
{
public void show(int a,int b)
{
int c = a + b;
System.Console.WriteLine ("the sum is "+c) ;
}
public static void Main (String []args)
{
obj ob=new obj();
ob.show(5,10);
}
}

}

METHOD WITH OUT RETURN TYPE AND WITHOUT ARGUMENT

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{

class obj
{
public void show()
{
System.Console.WriteLine ("hello") ;
}
public static void Main (String []args)
{
obj ob=new obj();
ob.show();
}
}

}

METHOD CATEGORIES

There are four method Categories

Here you have to concentrate well, because it is very important while doing programming.

There are four method categories
They are:

1) METHOD WITH OUT RETURN TYPE AND WITHOUT ARGUMENT
2) METHOD WITH OUT RETURN TYPE AND WITH ARGUMENT
3) METHOD WITH RETURN TYPE AND WITHOUT ARGUMENT
4) METHOD WITH RETURN TYPE AND WITH ARGUMENT

WHAT IS ARGUMENT

An argument given in the method, can take input through the argument list.

For example,

public int add(int x,int y)

public specifies the access modifier.
int specifies that the return type of this add method is an integer.
It is not necessary that it must be integer, as the parameter is given as int x, int y and it can be any of the data type. Where as we can determine what type it should return by proper conversion.
int x, int y are the arguments of this method.

RETURN TYPE

RETURN TYPE is the type of data, the method is going to give as the result.
i.e. what type of data is bevel
by the method after execution of that method
If particular method is not returning any data, then we have to mention it as void method.

for example,
public void add()
public specifies the access modifier, that this method is public
void specifies that this add method and is not going to return any value
add() method name

ACCESS MODIFIERS

This is to represent the modifiable level of any members used in our program. This determines whether the particular can be accessed are not.
In general there are 4 types:

PRIVATE: the member which is given with private modifier can be accessed only with in the containing class

PROTECTEDC INTERNAL: This can be accessed from the current projector the type inherited
INTERNAL can be accessed only from the current project.

PROTECTED: This be accessed from the class and the derived class.

PUBLIC: It is not restricted. It can be accessed from any where.

Saturday, November 7, 2009

Example program FOR Destructor

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication12
{
class A
{
public A()
{
Console.WriteLine("creation A");
}
~A()
{
Console.WriteLine("destroying A");
}
}
class B : A
{
public B()
{
Console.WriteLine("creation B");
}


~B()
{
Console.WriteLine("destroying B");
}
}
class C : B
{
public C()
{
Console.WriteLine("creation C");
}

~C()
{
Console.WriteLine("destroying C");
}
}
class MainClass
{
public static void Main()
{
C c = new C();
Console.WriteLine("object created");
Console.WriteLine("press enter to destroy");
c = null;
Console.ReadLine();
}
}

}

DESTRUCTOR

This is also instance of a class. This is the method called before the object is destructed by the garbage collector.
It is the function with the same name as the class but with ~ character.

SAMPLE PROGRAM for CONSTRUCTOR

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication12
{
class Super
{
public int height;
public int Widith;
public Super(int x, int y)
{
height = x;
Widith = y;
}
public int Display()
{
return (height * Widith);
}
}
class test
{
public static void Main()
{
Super s = new Super(10, 20);
int i = s.Display();
Console.WriteLine("Multiplication is :" + i);
Console.ReadLine();
}


}
}


Here public Super(int x, int y) is a method but the class name is also super therefor it is called as constructor.

CONSTRUCORS

Constructor is a type of method but it has some property.
The class name and the method name are the same.
It will not have return type but it can have parameters.
This is automatically called when a new instance of class is created, that is why it is called as constructor.

ABSTRACTION

This is the process of hiding the detail and showing the essential components.
In C# we can achieve abstraction with the help of abstract class. Abstract class, we can discuss in later sessions.

POLYMORPHISM

This means that one in many forms.
There are two types of Polymorphism:
Compile time Polymorphism and run time Polymorphism.
we can discuss this in detail in later posts.

INHERITANCE

The is the property by which one object acquiring the property of another object.
Here in dot net we can inherit the property of one class to another class.
Types of inheritance are:
Simple
Multiple
Multilevel
Hybrid
Hierarchal

Simple means deriving only one class from base class.
Multiple means deriving one class from more than one class.
Multilevel is more than one level of inheritance.
Hierarchal is deriving two or more class from one base class.
Hybrid means combination of all the above.

OOPS in C#.net ENCAPSULATION

This is very important feature in C# DOT NET LANGUAGE

ENCAPSULATION is just wrapping up of code and data in a single unit in class we are achieving.
That is in class we are binding code as well as data and it appears like a single unit.

SIMPLE PROGRAM USING CLASS OBJECT METHOD

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication11
{
class Program
{
public void show()
{
System.Console.WriteLine("hello");
}
static void Main(string[] args)
{

meth ob=new meth();
ob.show();

}
}
}
Here we have two methods. One main method and another show method.
In main method we have created one object for the class and we have accessed the show method.

METHOD

In normal English method is some thing like the ways to achieve some thing. Same can be applied here. It is the operations performed on data. It will take input values and parameters through it, and may or may not return values of particular data type.

A method is some actions that can be performed by class or object.
Method is the function owned by your class.

A method is the function of the containing class.


Syntax of method:



type method_ name (parameter-list)
{
// body of the method
}

OBJECT

OBJECT: This is the next important entity in programming. It is considered as the basic building block of object programming.
All the objects are created using the keyword ‘new’.


Syntax for creating a class is:


classname obj_name=new classname();

CLASS

The Class is an user defined data type. It is a simple abstract model to define a new data type.

It is the basis for object oriented programming .A class contains group of related variables and methods.

Syntax for class:

Class Myclass

{

// fields, operations and properties go here

}

BREAK ,CONTINUE,RETURN statements

BREAK:
Break is used to break the execution flow.


CONTINUE STATEMENT:
End iteration of inner-most loop.


RETURN:
Returns a value and or exits a method.

sample program of this will be given later when we learn other concepts.

FOREACH STATEMENT

foreach is one of the new thing in dot net, it will help the programmer in iterating each value in am array or collection.

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{

static void Main(string[] args)
{
int[] a ={ 1, 2, 3, 4, 5, 6, 7 };

foreach (int b in a)
{
Console.WriteLine(b);

}
Console.ReadLine();

}
}
}

Explanation:
Here I have used the statement, foreach (int b in a).
int b is the reference variable I am using for referring value in an array a. Where a is the array name which I have used in the above program.
I have used int b because here I am referring an integer array. So the datatype of the local variable depends on what type of array I am referring.

FOR STATEMENT

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{

static void Main(string[] args)
{

for (int i = 0; i <= 10; i++)
{
Console.WriteLine(i);
}

}
}
}

DO WHILE STATEMENT

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{

static void Main(string[] args)
{
int i = 0;
do
{
Console.WriteLine(i);
i++;
}
while(i<=10);

}
}
}

While sample Progarm

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i <= 10)
{
Console.WriteLine(i);
i++;
}

}
}
}

If else condition ststement

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 5;
if (a > b)
{

Console.WriteLine(" A is greater");
}
else
{
Console.WriteLine("B is greater");
}


}
}
}

Control statement sample Programs


Switch statement
First we are going to see a small porgram for switch case:

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 5;


Console.Write("Please enter your option: + or - ");
string str = Console.ReadLine();
switch (str)
{
case "+":
int c = a + b;
Console.WriteLine("the sum is " + c);
break;
case "-":
int d = a - b;
Console.WriteLine("the sum is " + d);
break;
default:
string stri = "not recognized";
Console.WriteLine(stri);
break;
}
Console.ReadLine();


}
}
}


Now we can see about

Console.ReadLine(); and Console.WriteLine();



Console.ReadLine(), is used to read the data from the console i.e, our peogram will get the input from the keyboard through console.


Here console is the predefined class and ReadLine is the predefined method in the Console class.
If you are a C programmer you can compare it with scanf and if you are a C++ programmer you can assume like cin>>
Console.WriteLine(), is used to print the out put in console WriteLine is the method inside Console class.
We can compare this with printf in c programming or cout<< in c++.

Friday, November 6, 2009

What is console application?



Console application is an application based on the command line. It consists of console window to see the output. The output looks like the command prompt.

There will be no graphical user interface.

When you select the console application in a new project, your window looks like the image as shown:

So all the required libraries are imported automatically:

using System;

using System.Collections.Generic;

using System.Text;

These are called namespaces , which are required for this console application to run.


namespace ConsoleApplication10


This is the name of your application.

You can give whatever name you want but it should be changed in the previous window. You can give the name there as well as you can change the location where your project is being saved using the browser button.

class Program

{

static void Main(string[] args)

{

}

}



This is the class which is created automatically. The class name is program and the method main is inside the class.

For time being, you practice writing the code inside the main method.

In later sessions I will teach you to create different classes and required methods.




Types of application In C#.net

In dot net we can create different types of applications like,

Console application

Windows Application

Web application

Device Application

Crystal Report Application

Class Library


As a beginner we are going to do all our programs in console application.

Select Visual Studio 2005


Follow the image to start doing simple programs.



Control Statement in C#.net

Next we are going to see the control statement in C#.net
The control statements can be classified into different types, they are:


Conditionals

 Switch
 if

Loop Statements

 while
 do while
 for
 foreach

Jump Statements

 break
 continue
 goto
 return

Operators Example

Arithmetic operator includes

+, * , / , % , + , –

Shift operators

<<,>>

Relational / Type testing operators

< , > , <= , >= , is , as

Logical operators

& , ^ , |

Conditional logical operators

&& , ||

Assignment Operators

= ,+= ,-= ,*= ,/= ,%= ,&= ,|= ,^= ,<<= ,>>= ,??



These are commonly used operators in C#.net.

Here we have to note that for Relational / Type testing operators, Logical operators, Conditional logical operators while it is under execution, the out put of the particular line will be Boolean i.e. either 0 or 1 i.e. either true or false.

This is used in checking a condition whether it is true or false.

Operators In C#.net

The operators available in C#.net are
Arithmetic Operator
Shift Operators
Relational Operator
Logical Operators
Conditional Logic operators
Assignment operators

How to use variables in C#.net?

While the program is under execution, the data is store in memory temporarily. Here the variable is the name given for the memory location, which contain the data of particular data type. Each variable must contain its data type and the variable name. In C# all the variables should be declared before using it.


Syntax : <> <>;

Example: int a;

Which means that a is of integer variable and can hold only integer data;
All the value type variable declaration follows the same syntax.

What is reference type?

These datatypes will store only the address as reference. The example of this data types are class, interface, array, Delegate. They are stored in memory in a heap manner.
In addition there are two predefined reference type they are object and string.

What is Value Type?

These are the datatypes which are predefined by the language developers. The memory occupied by this will be fixed depending on what type of data it is.
Examples of value datatypes are:
Byte, char, bool, sbyte, short, int, float, long etc. These datatypes take only predefined memory as assigned by the language developers. It is fixed and it cannot be changed. These are stored in memory in a stack manner.

Fundamentals of C# language to be known

Though in dot net we can develop using many languages, we are concentrating on
C#.net as it is the easy for C or C++ or any other language programmer to adopt.
Moreover it supports concepts like pointers, and operator overloading. So here we are learning C#.net. First we are going to see the data types used in C#.net.
we can broadly classify the data types into two types, they are value Types and Reference Type.

Thursday, November 5, 2009

what is visual Studio.net (VS.net)

Now What is VISUAL STUDIO .NET.
This is a development tool provided by Microsoft with rich set of productivity and debugging features.
It support C#,VB.net,C++,J#.
It contain number of tools.
It provide functionality by which we can provide server integration like sql SEVER.
It also provide web services.

compilation And Execution

Now let us see how the code is compiled to final out put.
Source code is compiled by compiler to form MSIL. It contains code & metadata. This is also called assembly or DLL. This is compilation phase.
Int execution phase another compiler is used which is known as JIT Just In Compiler. This JIT compiler will convert this MSIL into final output.

How Multiple Language Possible in Dotnet

How It supports Multiple Language ?
To understand this we have to know some concepts.
The first thing is MSIL. I have already explained what is MSIL.
For achieving this multiple language support, this plays very important role.
All the code which when compiled is converted into MSIL, what ever language we are using in dot net. when it is compiled, it is converted into MSIL. This MSIL is a low level language like assembler but it is object oriented.

CTS (Commen type system)
This is another important thing for multiple language support. It is the type system Built into CLR. It contains various types like int, Float, String etc.
And the operation of those types are made common.

CLS(Common Language Specification)
This is set of some specification, that each dot net language should follow. So if any language satisfies all the conditions of CLS, then that language can be use in dot net frame work.
This will ensure interoperability.
So if a language satisfies all the conditions in CLS, then that language can be used as dot net language.
The CLS is standardized by an organization called ECMA.

managed code in dot net

Managed Code

This is the code that is targeting the Common Language Run time.
In general the code which is getting executed in CLR is called managed code.
There are some features for this managed code.
They are:
1) object oriented
2)Cross language integration
3)Multiple version support
This managed code is represented in the form of intermediate languge Called MSIL (micro soft intermediate language).
It is also called IL (intermediate language).

Do you Know What Is Comman Languge Run Time(CLR)

What is inside the CLR.
CLR is common Languge runtime. It is very important in .net. The function of CLR is to manage the code execution at runtime. It provides all functionality for our .net application to run.
The features of CLR are:
1.Garbage Collection
2.Language Integration
3.Integrated Security

Garbage collection :
In general Garbage is some thing which is considered as waste material.
In dot net what is waste?
A variable which is declared and unused or any object which is not used is considered as the waste. The garbage collector will do allocate the memory allotted for this.
So memory can be saved. This is done by garbage collector.

Language integration:
This is another, very important feature
This will help to integrate more than one language .ie. we are using number of languages in our dot net frame work like C#.net,VB.net,J#.net. and many other third party languages.

What CLR dose
The main function of CLR is that it manage the code at rune time.
It is like a virtual machine. It dose memory management,Threading,etc

INTRODUCTION TO C#.net

What is dot net?
dot net is a frame work released by Microsoft in which the programmer can do programming in many languages like C#,VB.net,J#.net.

What did the Frame work contain?
The basics thing in the frame work are FCL and CLR. FCL is Frame work Class Library. ClR is common language Run time.
Fcl contain objected oriented Collection of predefined Classes.
Fcl Provide the core functionality, this help the programmer to achive the concep of code reuse.
All the necessary class and methods are defined in the frame work class library.
And the programmer can use the required class from the frame work class library.