Start C# here

Study C#.. Go ahead ! !

Tuesday, March 18, 2008

What are operators?

Operators are used to do perform some operations on variables.

We have four types of operators in C#.
  • Arithmetic Operators,
  • Relational operators ,
  • Logical operators and
  • Unary operators.


Arithmetic Operators :-

Arithmetic Operators are used to do mathematical operations


The basic arithmetic Operators are

+ for addition
- for subtraction
* multiplication
/ division
% modulo operator(This operator is used to return the Remainder of two numbers).



Relational Operators:-


Relational operators are mainly used for checking conditions between two or more variables.

The relational operators we use in C# are..

> Greater than(Checks which number is greater)
<>= Greater than or equal to(Checks whether the given number is greater than or equal to the second number)
<= Less than or equal to(Checks whether the given number is less than or equal to the second number) == Equal to(Checks whether the second number is equal to the first number) != not equal to(Checks whether the second number is not equal to the first number) Logical Operators :

Logical operators are used to check logical conditions with a block of two or more relations
Logical operators we use in C# are AND, OR and NOT.

  • AND operator is represented with && (two ampersand)
  • OR operator is represented with || (two pipes) and
  • NOT operator is represented with !(Exclamation).

eg:-
((iNum1>iNum2) && (iNum1!=0))
In the above example we are checking whether the first variable is greater than the second variable AND iNum1 is not equal to ZERO. Here if both the conditions are true only the condition will execute.


How Logical operators work.

AND(&&)
&&(AND) operator becomes true only if all the conditions are true. If any of the conditions are false && operator returns false.

OR(||)
||(OR) operator becomes false only if all conditions are false. If any of the condition becomes true || operator becomes true.

NOT(!)
!(NOT) operator becomes true if the condition is false and false if the condition is true.


Unary Operators

Unary operators are used to add or subtract a variable by 1.
We have mainly two types of unary operators
Increment and
Decrement.


Increment operator is represented by ++ and decrement operator is represeted by --.
Increment operator adds a variable by one and decrement operator subracts a variable value by
one.

i++, i--.

Unary operators can be used in two ways POSTFIX and PREFIX.

In a postfix notation the unary operator will be placed after the variable(i++)
In a prefix notation the unary operator will be placed before the variable(++i)



Assignment Operator

An assignment operator is used to assign a value to a variable. An = (equal to) symbol is known as an assignment operator.

int i=10;
means assigning the integer variable i a vlaue of 10.

What is a Variable?

Variable is a name given to a memory location.
Variable will be having a memory address value

For eg:-
int iNum1
char cName
float fNum etc..

Here, int,char,float represents the Data type to be used and iNum1,cName,fNum are the variable names given for those DataTypes.



Rules For Declaring a Variables :-

  • A Variable name Should Start with an Alphabet
  • Key Words representing the program should not be used as variable name.
    • for eg: numeric, int, console etc.. should not be used.
  • Special characters except an underscore( _ ) are not allowed
  • Variable names should not start with a number.

Friday, March 7, 2008

Data Types in C#

As of now we have studied how to start program using C#.

Now we will check some other programs in C#.

What do you mean by a Data Type?
In simple words Data Type means the type of data. The data may be mainly of three types. They are
Integer, Character and Float. These are also called base data types.

Integer are used to store integers or numbers without decimals.
eg:- 1,123,1452,9999 etc...

Characters are used to accept letters. Basically a character Data type can accept only one byte. So, it will store only one Letter.

eg:- a,A,b,c,d etc....
If we write -AB- it will return an error...

Float is used to accept Floating numbers or numbers with decimals..

eg:- 1.8F,21.3F etc

Some data types are :-

Name
. . . . . Description
byte . . . . .. . 8-bits '
sbyte . . . .. . 8-bits
int. . . . . . . .. 32-bit
uint. . . . . . .. 32 bit
short. . . . . ..16-bit
ushort . . . .. 16-bit
long. . . . . . .. 64-bit
ulong. . . . . .. 64-bit
float . . . . . .. 32-bit
double. . . . .. 64-bit
decimal . . . . 96-bit
bool . . . . . . . true and false
char. . . . . . . 16-bit

Tuesday, March 4, 2008

First Program in C#

So, we are going to start with C#. C# is an Object Oriented Language developed by Microsoft Corporation for working with .NET platform. For working with C# you must have any version of Visual Studio installed on your Computer. For the latest case you will get Light Components like Microsoft C# Express Edition to Download from Microsoft which is absolutely free.
Now we can see a simple program in C#.

-------------------------------------------------

using System;

namespace smilu
{
class cHello
{
public static void Main(string[] args)
{
Console.Write("Hello World");
}
}
}


--------------------------------------------------------


I know that if you are a fresher to C#; you may be wondering seeing the Code. There is nothing in much in the above code....

Let's be now familiar with what is written in the above code.

In the above code go from the begining...


The first line is

using System;

The using statement is used to add a NAMESPACE into this program.
Namespaces are collection of Classes. These Namespaces are used to avoid Class Name Conflicts. ie, there may be situations where we must have two or more CLASSes with the same Name. So, inside a program, it will not allow to create two classes with the same name. So there exists a problem. So to avoid these types of conflicts we use Namespaces. So we create a namespace and add the classes inside the namespace and we create the other class with the same name in another namespace.

In the above program we have added System namespace into this program. The System namespace contains CLASSes for implementing base datatypes.

Now the second step

namespace smilu

This step is used to create a User Defined namespace which contain classes. Here we have created a Namespace in the name of "smilu".

Then you must have noticed curly brackets throughout this program. These are the Bodies of Namespace, Classes and Functions.

The Third step

class cHello

As I have said in the earlier topics that C# is an Object Oriented Language. So an Object Oriented Language(OOL) must have a class. Without class we cannot say a program is purely Object Oriented(refer). So, in this step we are creating a CLASS with name "cHello".
We will be writing rest of the Codes inside the CLASS

The Fourth Step

public static void Main(string[] args)

or

public static void Main()


This is the most important Function of a Program. Without the Main() function a program will not Execute. We write the Main() function in C# like this. All the things written there are not necessary. We will check each keyword in the Declaration.

public:
public written in the function refers it Access to others.It is known as "Access Specifier" ie we have Given a public access to the Main function,so anywhere within the program we can access this function.

Static:
C# is an OOL. So member variables and member functions can only be accessed through objects. Here in the case of Main it is the heart of a program. When we execute a program the first thing which gets executed is the Main() function. This is the first class so Object for this class can only be created inside the Main() function. So we must have to get the program started by executing the Main() function. That is why we declare a Static keyword to Main() function. So it is not necessary to create object for the class.

void:

void means NOTHING.... void represents the return type of the Main() function. ie, what the main should return to the program. Here in this case we doesn't need anything to be returned.. so, we have given the return type as void.


Main(string[] args) :
string[] args is used to store Command Line Arguments(if any). Command Line Arguments are arguments which we pass to a program while its execution with its Name.


The Fifth Step

Console.Write("Hello World");

Here we are creating Console application ie, we create a DOS based program. System.Console is a class which contains definitions for Write(), Read() etc... We mainly use
Write(); For writing in the same line(output)
WriteLine() This is used to Write in a string and set a break to the Line after that.
Read() Read a single line
ReadLine() Reading the strings.

Here in the above code inside the write function I have written "Hello World". The text inside the double quotes("") represenets the text to be printed as the output. What all we are write inside the "" of the Write() or WriteLine() functions will be printed as that itself exept some Escape charactors like...
Commonly used escape charactors are...
\n - For new Line
\t - For a Tab Space
\b - For a BackSpace etc...



NOTE:- C# is a case sensitive Language. So it is necessary that you write codes in appropriate cases itself as written above.



After you type the program save the file with a Filename with extension ".cs"(eg:- Hello.cs).
For executing the program take the Microsoft Visual Studio 2005 Command Prompt from

START->PROGRAMS->MICROSOFT VISUAL STUDIO 2005->MICROSOFT VISUAL STUDIO TOOLS->MICROSOFT VISUAL STUDIO 2005 COMMAND PROMPT.



Now navigate to the Folder you have saved the File.

Compile the Code.

For Comiling you have to do like this .. if the file name is Hello.cs

In the COmmand prompt write " csc Hello.cs " and Press enter

If the compilation returns Successfully write the File name and press Enter
like

" Hello " and Enter.

You will get an output as

Hello World

Saturday, March 1, 2008

What is Object Oriented Programming?

So, Lets start from the First. Before we all start one must be aware of what is Object Oriented Programming? So do you know what is Object Oriented Programming?
Object Oriented Programming(OOP)


Object Oriented Programming was first introduced in 1960’s.The main reason behind this programming is to make the program user friendly. OOP concept is related to the real-world system (i.e concept of Classes and Objects).


What is a CLASS?
Class is just a Virtual concept of a collection of related items together which we cannot see or feel.In programming we create Classes to define collection of related variables and functions.
Class is a user defined Data Type


What is an Object?
In simple words -- Object is an Instance of a Class. We can access a Class only through its Objects.Without objects we cannot say a Class is existing.A thing which has the properties of a class is known as Objects.

For eg:- Car is a Class and Tata Indica is an Object of the class Car
Objects….


We generally identify object by its State, Behavior and Identity.
If we take a car Indica its state is its color, wheel, body, etc….

Its Behavior is moving, changing gears etc…
And the identity of the car is its Number lets say KL-7 AD 2544.

Here state and behavior may be same for many objects but the identity of object will not be same for other object.


Characteristics of OOProgramming…

The main characteristics of Object Oriented Programming are:-

  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism



INHERITANCE

The ability of a class (Derived Class) to derive or Inherit the properties of another class (Base Class) is called Inheritance.Inheritance helps in the reusability of code hence making the program smaller and easy to use.
Eg:- Parent child relationship. Child is Inherited from its parents


ENCAPSULATION


Process of Hiding the data from a user is called Encapsulation; also called Data Hiding.


ABSTRACTION


The process of showing essential features without giving any explanations is known as Abstraction.


POLYMORPHISM


The name Poly Morphism means Many Forms. It is the process by which we create same things with different behaviors.
Eg:- Function overloading…
Eg Same person behaving in different ways. Make the person you teach as the example and ask him whether he behaves like the same when he is in front of his Principal and friend…. You can change the principal to Police also so the student will really understand what is poly morphism.