Pages

Thursday, December 30, 2010

30/12 Operators in C#

DATE 30/12/2010 Thursday

Operators in C# à

Arithmetic –

+, -, *, /, %

Assignment –

=, +=, -=, *=, /=, %=

Comparison –

++, !+, <, <=, >, >=, is, as, like.

Concatenation –

+

Increment and decrement –

++ , --

Logic –

&& , ||, ^

Conditional statement: - A block of code which gets executed basing on a condition is a conditional statement.

There are two types à

- Conditional branching.

- Conditional looping.

1. Conditional branching – this statement is also allow you to branch your code depending on whether certain condition were met or not. C# has two constructs for branching code, the if statement which allows you to test whether a specific condition is met and the switch statement which allows you to compare and expression with a no. of different values

If ( < condition > )

< stmts > ;

else if ( < condition > )

< style="mso-spacerun:yes"> >;

----------------------.

else

< stmts >;

Program of if statement –

using System;

class IfDemo

{

static void Main ()

{

int x, y;

Console.Write("Enter x Value :");

x = int.Parse(Console.ReadLine());

Console.Write("Enter y Value :");

y = int.Parse(Console.ReadLine());

if (x>y)

Console.WriteLine("x is greater");

else if (x

Console.WriteLine("y is greater");

else

Console.WriteLine("Both are equal");

}

}

Switch case – the expression of switch case.

switch ( < expression >)

{

Case < value > :

< statement >;

Break ;

.-----------------.

default :

< stmts >;

break;

Using break after each case is mandatory in C# and also break has to be used after default.

Program of switch –

using System;

class SwitchDemo

{

static void Main()

{

Console.Write("Enter a student no. (1-3): ");

int sno = int.Parse(Console.ReadLine());

switch (sno)

{

case 1:

Console.WriteLine("Student 1");

break;

case 2:

Console.WriteLine("Student 2");

break;

case 3:

Console.WriteLine("Student 3");

break;

default:

Console.WriteLine("Invalid student no");

break;

}

}

}

Condition loop –

C# provides 4 different loops that allows you to execute a block of code repeatedly until the certain condition is mat those are –

· for loop

· while loop

· do …. while loop

· foreach loop

Every loop required three things in common

1. Initialization which sets the starting point of loop.

2. Conditionwhich sets the end of a loop.

3. Iterationthis tacks you to the next level of the loop, either in forward or backward direction.

FOR LOOP

for ( initialize ; condition , iteration)

< statement > ;

for ( int x =1 ; x <= 100; x++)

console.WriteLine (x);

WHILE LOOP –

while ( condition)

< statement > ;

int x = 1;

while ( x <= 100)

}

Console.writeLine (x);

x++;

}

DO…… WHILE LOOP

This is similar to while loop but for the first execution of the loop it doesn’t require any condition test. After completing the execution for one time then it checks for the condition to execute for next time.

do

{

< statement >;

while ( condition );

int x = 1 ;

do {

Console.WriteLine(x);

x++;

} while (x<= 100);

FOREACH LOOP it is a special loop that has been design for the processing of arrays and collection.

Note- array is the set of similar type value, and the collection is the set of dissimilar type value.

foreach (type value in coll / array)

{

< statement>;

}

JUMP STATEMENTC# provides a no. of statements that allow you to jump immediately to another line in a program w have four different jump statement support in C# those are –

1. goto

2. break

3. continue

4. return

GOTOIT allows you to jump directly to another specified line in the program indicated by a label which is an identifier followed by a colon (;).

goto xxx;

console.WriteLine (“ Hello”);

xxx;

Console.WriteLine(“ goto called”);

BREAKit is used to exit from a case in a switch statement and also used to exit from for, foreach, while and do…..while loop. Which will switch the control to the statement immediately after and of the loop.

for ( int i=1 ; i<= 100; i++)

{

Console.WriteLine (i);

if (i== 50)

break;

}

Console.WriteLine(“ End of the loop “);

CONTINUE this can be used only in the loop statements which will jumped the control to iteration part without executing the statements present after it.

for ( int i=1 ; i<= 100; i++)

{

if (i=7)

continue;

Console.WriteLine (i);

}

Console.WriteLine(“ End of the loop “);

RETURNIf a jump statement which can come out of a method or function if required in the mid of it execution as well as it is also capable to carry a value from the method or function outside.

program using return -

using System;

class RetDemo

{

static void Main()

{

Console.Write("Enter a numeric value: ");

int no = int.Parse( Console.ReadLine() );

if ( no <>

return;

for(int i=1;i<=10;i++)

Console.WriteLine("{0} * {1} = {2}", no, i, no * i);

}

}

29/12 Data types in C# -



DATE 29/12/2010 Wednesday

using System;

class VarsDemo

{

static Void main ()

{

int x, y, z;

console.Write(“enter x value:”);

x = int.Parse(console.ReadLine());

console.Write(“Enter y value :”);

y = int.Parse(console.ReadLine());

z = x+y;

console.WriteLine(“Sum of {0} & {1} is : {2}”,x,y,z);

}

}

The ReadLine method return the value what it read in string format only because the return type of the method is string.

Parse is the method that is capable of converting astringe expressions into a given type on which the method is called provided it is convertible or else raises an error.

Int x = int.Parse (“100”);

float f = float.Parse (“3.14”);

bool b = bool.Parse(“true”);

int y = int.Parse(“1a7”); // invalid

# Data types are classified into two types –

1. Value types

2. Reference type

Value types store the data in stack which is the place where data store in fixed length such as int float etc.

1 Every program has its own stack and no other program shares it.

2 Stack works on the principal “first in last out” and it is under the control of operating system which doesn’t provide automatic memory mgmt but faster in access.

Reference type – reference type are store on Heap memory which is the other place where data can be stored. C# support two predefined reference type objects and strings and .net the Heap is now more managed and called as managed Heap, which will be under the control of garbage collector.

Nulleble Value types (2.0):- it is a new feature that has been added in C# 2.0 which allows storing of null value in value types that is not possible in the version before 2.0, which gives more flexibility while communicating with data bases because under databases null values can be stores both in value types and reference types also.

Ex- string Str = null; //valid

Int x = null; //Invalid

Int? x = null; //valid

Note: - to declare a value type for storing null value we need to suffix the type with “?”.

Implicitly typed variable (3.0) :- It is a new feature that has been added in C# 3.0, which allows declaring a variable using the keyword “var”. the type of declared variable is identified in run time depending upon the value we assign to the variable.

Var x = 100; //x is an int variable.

Var f = 3.14; //f is a float variable.

Var s = “Hello”; // s is a string variable.

Note- Declaring a variable here without assigning a value is not allowing.

Var y; // invalid.

Program-

using System;

class TypeDemo

{

Static void Main()

{

var x = 100;

console.Write.Line(x.GetType());

var s = “Hello”;

console.WriteLine(s.GetType());

var f = 3.14;

console.WriteLine(f.GetType());

}

}

Note :- get type is a predefined method that return the type of a given variable or object.

Boxing & Un-boxing :- If at all a value type is assign to a reference type variable and sent to managed heap we called it as Boxing

int x = 100;

object obj = x; // BOXING

Un Boxing is reverse a boxing which allows reassign of refrance type back to corresponding vale type again but here an explicit type casting is required.

int y = (int)obj; // un - Boxing

28/12 System.console.WriteLine-

DATE 28/12/2010 Tuesday
System.console.WriteLine-

Console is a predefined class under the base class Libraries which provides standard IO functionality that can be performed on standard IO devises like - keyboard and monitor. The class has a set of static method like- Write, WriteLine, ReadLine etc.

Because the method s are static we don’t required the object of class for Invoking them what we needs only the name of the class.

System is a “Namespace” which is a logical container of types. Logical containers are used in two different cases.

1. To group related items i.e. under base class library we have number of classes that performs related operation .To group all related classes they were put on the separate name spaces.

2. We also used them to overcome the problem with naming collision i.e. defining of multiple classes with the same name can be done by putting them under separate name spaces and then refer the class with name space.

System.Runtime.Remoting.Channel.Tcp;

Importing a Namespace: - If at all a class is defined under a Namespace must and should we need to refer the class only with the help on name space in which it was present. This may be a complication if we want to consume the class for multiple times. To overcome the problem you can use the option importing on name space i.e. on the top of the program if we write “using ” within the class we can consume all classes under that Namespace without prefixing it namespace again.

Using System;

class UsingDemo

{

Static voidMain()

{

console.WriteLine(“Importing a namespace”);

}

}

Data types-

Integer type-

byte

System. Byte

0-255

short

System.short

-32768to32767

int

System.int

-2^31to 2^31-1

long

system.long

-2^63 to @^63-1

sbyte

system.sbyte

-128 to 127

ushort

system.ushort

0-65535

uint

system.uint

0-2^32-1

ulong

system.ulong

0-2^64-1

Decimal or float types:-

float

System.single

4 bytes

double

System.double

8 bytes

decimal

System.decimal

16 bytes

Boolean type:-

bool

System.boolean

true or false

Character types:-

char

System.Char

2 byte

string

System.string


The size of char type has been increased to 2 bytes for providing the support to Unicode characters i.e. all the languages other than English which are represented with a Unicode value.

String is a variable length type which doesn’t have any fixed size. Its size depends upon the value we assign to it.

Root Type:-

Object

System.object


Object type is capable of storing any type of value in it like – Character, Integer, and Float, Boolean etc. it is also variable length type.

[ <> ] [const][read only]<><><= value] [,…n]

int x;

int y = 100;

string S1,S2,S3;

public const float pi=3.14f;

double dd = 324.879;

public readonly decimal d = 456.987m;

modifiers or special keywords like- public , privet, static etc.

Note- the default scope for member of a class in c# is privet.

A constant and read only variable or same which cannot be modified after assign a value.

Every numerical value by defoult is considered as int and decimal value is considered as double so to represent a float velue suffices the value f and m for decimal value.