Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 47

C# programming language

First c language
Introduction

• C# .net is the most powerful programming


language among all programming language
in .net, because c#.net will contain all the
features of c++,VB 6.0 and java and additional
features.
• In c#.net, the symbol # must and should be
pronounced by “sharp” only because micro
soft has taken the symbol from musical note,
is most power full programming language
among all programming languages.
• it was an object oriented programming
language developed by microsoft as part
of .net intiative & later improved as a standard
by ECMA & ISO.
• Anders Hejlsberg leads development of the
language which has a procedural & object
oriented syntax based on c++ syntax &
includes influences from most notably Delphi
& Java with a particular emphasis on
simplification.
History of the language
• During the development of the language ,class
libraries were originally written in the language is
called simply managed ‘C’ (SMC). In jan 1999
Ander Hejlsberg found a team to build the
language had been named C# & the class libraries
as well as ASP.net runtime has been ported c#.
• C# principle designer & lead architect at Microsoft
is Anders Hejlsberg ,who was previously involved
with design of visual J++,Delphi & Smalltalk draw
the fundamental of CLR, which in turn draw the
design of c# programming language itself.
Design Goals

The ECMA standard list these design goals to c#


• It is intended to be a simple, modern, general
purpose & object oriented programming language.
• It should include strong type checking, array
bound checking detection of attempts to use
uninitialized variables, source code portability &
automatic garbage collection.
• It is intended for use in developing s/w
components that can take advantage of distributed
environments.
• As programmer portability is very important
especially for those programmers who already
families with c & c++ ,c# is best suitable
• It is intended to be suitable for writing app’s to
both hosted & embedded systems
• Version of c# :-1.0,1.5,2.0,3.0,4.0,5.0
• Feature of new in c# 2.0 :-
1. Partial class:-which allow class
implementation across more then one source
file
2. Generics of parameter types:- static classes
that can’t be instantiate and that only allows
only static members
3. Anonymous Delegates
4. The accessibility of properly assessors can be
best independently
5. Coalesce operator(??) return the first of its
operands which is not null/null if no such
operant exit.
New features in c# 3.0
• Language integrated query.
• Obj initializes & collection initializes .
• Anonymous types
• Implicitly typed arrays
• Lamda expressions
• Automatic properties
• Extension methods.
programming under diff language
• Structure of ‘C’ program:-
Collection of functions and variables
Void main()-(entry point)
{
call the functions from here
}
• Procedural program language :-‘C’ is a collection
of function and variables where these function
were explicitly called in main functions.
• Most of the programming language were
designed in such a way they state their
execution form main only.
• Procedural programming language are the lack
of features like security and reusability.
• To over come the drawbacks of procedural
programming a new approach has been
introduce as object oriented program.

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();

crate the object of class


using the object invoke the members in class
}
}
• When Microsoft designed c# it has followed the
same guideline prescribed by java inters of main
so in c# also main method gets declared as static
• C# structure also will be same as java program
syntax to c# :-
[<modifiers>] class <name>
{
statements
}
• Modifiers were optional which can be keywords
like public, private, static etc.
• Note :- keywords are lower case
• C# is a case sensitive language it has adopted
few convention while defining the language i.e
all the keywords should be in lowercase &
the predefined Classes &Method adopt
proper case convention
• history of c#:-
Syntax of Main method
Static void main([string []args])
{
}
Main should be static if at all we want to start the
execution from here. so that object is not require to
invoke the method
• Main can be either a value returning/non value
returning. if we wan to return a value it can be only
a int value
• We can pass string array as parameter to be a main
method if required
C# programming language
• It was an object oriented programming
language develop by micro soft as part of .net
initiative and laterimproved as a standard by
ECMA &ISO.
• Anders hejlsberg leads development of the
language which has a procedural & object
oriented syntax based on c++ syntax and
include influences from several othr
programming languages .most importantly
Conditional statements

A block of code that executes basic upon a condition is a


conditional statement, They are of 2 types
1. Conditional branching
2. Conditional looping
Conditional branching :-These statements allow you to
branch you code depending on where certain
condition were meet or not.
C# has 2 construct for branching code, the “if”
statement which allows you to test whether a specific
command is met and a ‘switch’ statement which
allow you to compare a expression with a number of
different value
• Syntex:-
If (<condition>)
<statement>;
Else if(<condition>)
<statement>;
---------------
--------------
else
<statement>
Ex:-
Switch case

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

• These represent the data in the form of rows


and columns.
<type>[,]<name>=new<type>[row,cols];
Int[] arr=new int[3,4]; (or)
Int[,] arr;
Arr=new int[3,4]; (or)
Int[,] arr={lit of values}
Ex:-
2Darry
Jagged Array
• These are similar to a 2D array. were as in a 2d
array all the rows will have fixed set of columns
but in case of jagged array each raw will
contain different number of columns.
• These are also refer as array of arrays because
it’s a combination of multiple single
dimensional array.
<type>[][]<name>=new<type>[rows][];
Int [][] arr=new int[3][]; (or)
Int[][] arr={list of values};
• In cased of a jagged array in the initialized
declare we can only specify the no. of rows to
the array
Ex:- int [][] arr= new int[4][];
• Now to printing to each row we need to
specify the number of cal to the row. Ex:-
arr[0]=new int[5];
Jagged Array1
arr[1]=new int[6];
arr[2]=new int[8]; Jagged Array2

arr[3]=new int[4];
Boxing and Unboxing

Srno Boxing UnBoxing


1 Converting a variable from value Converting a variable from reference
type to reference type type to value type
2 Supports 2types:- Supports only 1 Type
Implicit Boxing Explicit Unboxing
Explicit Boxing
3 Boxing is 20 times costlier than Unboxing is 4 times costlier than
normal Initialization normal Initialization
Boxing is 20 times costlier than normal
Initialization, because whenever a boxing is
done following tasks will be performed
internally.
1. Runtime will search for the respective data
within the stack
2. A Copy of this value is made into Heap
3. Reference to this copy is maintained from the
object variable.
Unboxing is 4times costlier than normal
initialized because, when unboxing is made
following tasks are performed internally.
• Object referenced value is searched within the
Heap.
• A copy of this made into stack.
Method

• A named block of code is refers as a method or


subprogram it can be either value returning or
non-value returning.
• A method is nothing but a action which has to
perform by the user.
Syntax:[modifier]<type|void><name>([parameter
def’s])
{
<statement>
}
• Modifiers are optional parameters which were
special keywords like public, private,
virtual ,overwrite etc.
• Method can be either non value returning /value
returning.
• If it was non value returning we use void & we
use type to specify the type of value it returns
• Param definition are used for passing parameters
to a method which was also optional. We can
pass parameters to a method as following
• [ref|out]<type><var>[,..n]
• We use the ref|out keyword for passing a
parameter using passby ref mechanism
• Methods has to be defined under classes as per the
rules of encapsulation
• To execute a method they need to be explicitly
called using the object of class in which they were
defined .so object of the class has to be created as
following
<class><obj>=new<class>([<list of values>])
Program p=new program() //p is obj (or)
Program p; //p is variable
P=new program(); //p is obj
• Note:- The object of a class can be created any
where in a class .it can also be created in other
classes also.but generally we create object
under main() because, it was the entry point
of the program.
• Write the program to using different types of
input output parameters
Parameters are 2 types

• Input parameters & Output parameter


Input parameters are used for passing a value
int the method for the execution of the
method
We pass input parameter by using a mechanism
(pass-by-value)
Output parameter are used for returning a value
after the execution of the method. which can
be passed to a method using a mechanism
(pass-by-pointer) “implicit pointer”
• Note :- An implicit pointer in the sense here
address are managed internally by the system
without

You might also like