Professional Documents
Culture Documents
C# Programming Language
C# Programming Language
First c language
Introduction
members member
class
Procedural prog Object oriented prog
• To over come the members defined under the
class we require 1st create an object of the class
• Class and its objects:- A class is a user defined
type, we can never consume a type directly
because type never occupies space in memory.
To consume a type 1st we require to create a
copy of the type which allocate the memory
and the we can consume it ex:-
• Int =100 ;//invaild
• Int x=100; // valid
• Class is also a type so,it can’t be consumed directly to
consume it,1st we require to create a copy of class type
also which were know as object .
• Structure of c++ prog:-
Class example
{
collection of function & variables
};
Void main()
{
create object of class
using the object invoke members in the class
}
• As c++ is an object oriented language we define
the member under the class to invoke the
member we need to create object of class under
main & then invoke the members.
• C++ language suffers from the criticism it was not
fully object oriented. because as function
standard every member of a class should be
defined inside the class but main function in c++
program will be defined outside the class. If it is
inside the class the execution of program will not
take place .main function require the object of the
class for execution which is crate under it only
• To over come the problem while designing
java, they have designed in such a way not
even a single line of code can be defined out
side of the class & to resolve the problem with
main.it introduced static members which
does’t requir the object of class for invocation
& so if main is declared a static without object
of it (main fun) can start the execution
Structure or java program
Class example
{
void m1(); m2();
collection of methods & variables
static void Main()
{
example obj=new example();
obj.m1();obj.m2();
Switch (<expression>)
{
case<value>
<statement>
break;
-------------
----------
defult
<statement>;
}
Ex:-
Conditional looping
C# provides 4 different loops that allow you to
execute a block of code repeatedly until a
certain condition is met those are.
For loop
While loop
Do ----while loop
For each loop
• Every loop require 3 think is common
(initialization, condition, iteration)
• Initialization:- which state the starting point of
loop.
• Condition:- which state the ending point of the
loop
• Iteration :-which takes you to the next cycle
either forward or backward direction.
• For loop :-
For (initilization;condition;iteration)
<statements>;
For(int i=1;i<=100;i++)
Console.WriteLine(i);
While condition Do while loop
While(condition) Do
<statement>; { <statement>;}
Ex:- int x=1; While(condition);
while(x<=100) Ex:-Int x=1;
{ Do
Console.WriteLine(x); {Console.WriteLine(x)
X++ X++}
} While(x<=100);
foreach
• These are specially design processing the values of
arrays and collection. Array is a set of similar types
value were as collection is a set of dissimilar
types.
For each(type var in call ! array)
{
<statements>;
}
foreach (int x in arr)
{console.w(x)}
Jump statements
• C# provide a no of statements that allow you to
jump immedietly to another line in a prog those
are (i)go to (ii)break (iii)continue (iv)return
(i)Goto:- it allows you to jump directly to another
specified line in the prog indicated by the table
which is an identifier followed by a colon ex:-
goto xxx;
Console.WriteLine(“hello”);
xxx:
Console.WriteLine(“goto called”);
• Break:- it is used to exit from a case in a switch
statement & also used to exit from for, for each, which
& do while loops, which will switch the control
immediately after end of the loop.ex:-
For(int i=1;i<=100;i++)
{
Console.WriteLine(i);
if(i==50)
break;
}
Console.WriteLine(end of the loop);
o/p :-it print from 1-50 & end of the loop
• Continue:- this can be used only in the loop
statements which will jump the control to the
iteration path without executing the
statement present after it.ex:-
For(int i=1; i<=100; i++)
{
if(i==7)
continue;
Console.WriteLine(i);
}
• Return:- it is used incase of a function /a
method to come out of a method. Ex
Array
• It was a set of similar type values that can be
stored sequentially. C# support 3 types of arrays
(i) Single dimensional
(ii) Two dimensional
(iii) Jagged arrays
In c# arrays can be declared as fixed length or
dynamic, fixed length arrays can store a
predefined no. of items while size of dynamic
arrays increases as you add new items to the
array
Single dimensional array
• These arrays store the values in the form of a
row which were declared as follow:
• Syntax:
<type>[]<name>=new<name>[size];
Eg: int[] arr=new int[4]; (or)
Int[] arr;
arr=new int[4]; (or) The initialization of an array gets
performed only with the use of new
Int[] arr={list of values}; operator (or ) assignment
array
of values to the
Ex:
• It has been specially designed to access values
of an array or collection. for each iteration of
the loop one values or array is assigned to the
variable of the loop in a sequential order
Difference between for & foreach
• The variable of the loop refers to index of your
array. their as in case of foreach loop the
variable of the loop referred value of the array
• The loop variable will allow be int only their as
foreach loop the loop variable will be
according to the type of value in the array
• Note:- array are reference type as we are
variable length type for which the size can be
specified after declaration. Array1 stringArray2
, array Obj, ChangeSizeofarrAtRun Time
Array class
• Array class:-
Sort(array)
Reverse(array)
Copy(src,des,n)
getLength(int)
Array Method
Two dimensional array
arr[3]=new int[4];
Boxing and Unboxing