Download - Wel come To Seminar On C#
Wel come To Seminar
On C#
.Net Framework
Consists of Two things
CLR(common language Runtime)Framework above OSHandles execution of .Net Applications
while maintaining performance
FCL(Framework Class Library)Thousands of Classes for Network
Prog, Data access ,Threads ,common DS,
etc Pure OO design
MSIL(Microsoft Intermediate Lang.)
Prog .in any .Net language is converted to MSIL first & not into native code
Os & hardware independent code
Interpreted by CLR
Similar for all .Net Language.
.Net LanguagesC# , C ,C++ ,Java Script ,VB , etc
How to decide ?CLS(common language specification)
# small set of Rules Eg. no global func decl , no pointers ,no multiple inheritance, etc
CTS(Common Type System) # set of standards
Eg Every .net lang. must hav some basic datatypes
JIT(Just in time ) compiler Using JIT , CLR converts MSIL code into
native executable code
Aware of each OS so can optimize code & makes Robust applications
Reuses the code when used again So performance increases when
program is run for a long time
CLR Execution model
VBSource code
Compiler
C++C#
CompilerCompiler
AssemblyIL Code
AssemblyIL Code
AssemblyIL Code
Operating System Services
Common Language Runtime
JIT Compiler
Native Code
Managedcode
UnmanagedComponent
Evolution of C# C++ Gave performance & efficiency
BUT No automatic Garbage collector Difficult to handle very large program Hard to work at windows application level
VB easy to work at windows appli. Level
BUTdifficult to work at system level
Class & objectsSimilar to structure but with differenceHas attributes & methods
Eg. Class Fruit {int price;float weight;public void getAttrib();
}Attributes (characteristics) price, weightMethod( behaviour) getAttrib()Fruit mango; mango is reference to classmango =new Fruit( ); mango refers to an
object of class Fruit & contains address of that object
A Short Programusing System;namespace Me {class Hello{
String name=“Yash”;static void Main( ) {
Console.WriteLine("Hello world");
console.WriteLine(“Hello{0}”,name);
} // Main ends } // class Hello ends } // namespace Me ends
Understanding concepts
NamespaceCollection of logically related classes
similar to concept of Package in JAVA difference :-Unlike JAVA ,Classes of
same namespace may not be in same folder
Main AdvantagePrevents name conflicts of classes
in program
Here
Me is the namespace So in future if we want to reuse class Hello we can use by calling it as Me.Hello
Understanding conceptsUsing Keyword
Similar to import keyword in JAVA
Tells program which classes r to be used
HereEg. using System –tells program that we want to make use of classes inside system namespace but remember that does not include classes inside sub
namespaces just like Java
Understanding conceptsMain Method
Unlike JAVA & C , there can be more than one main method in a C# program
But execution will start from the main method with capital ‘M’
Unlike JAVA , Name of the program need not be same as class name containing Main ().
Understanding concepts
Console ClassClass of System Namespace
Methods WriteLine() = printf( ) , ReadLine() = scanf( )
Eg. WriteLine( “ Hello {0} , How r u ? ”, name ) ;
printf in C
OR
WriteLine(“Hello “+name+” , How r u ? “) ;
println in JAVA
Diff . Bet’n Structure & Classvalue type while class is of reference type
may contain constructor except no argument constructor
neither inherit another class nor can they be implemented
can implement interfaces
implicitely inherited from System.Object class
instances can be created with or without using new keyword
When kept small in size a struct is more efficiently used by the system than class
Abstract classan incomplete classone or more abstract (incomplete methods)i,.e method with signature but no implementationcan’t be instantiatedi.e its object can’t be created but reference can beCan be inherited Its subclass must implement all abst. Methods of Abstract class
Eg . Abstract class A {int a1;public abstract int add(int x , int y);
}class B : A {
int b1;public int add(int x, int y) {int z=x+y;return z;
}}
if A a = new A( ); Error
Object Oriented LanguageInheritance
Eg . Class A {
int a1;int a2;public void Get();
}Class B : A{
Object Oriented Language
Inheritance
Like Java & unlike C++ , C# does not allowmultiple inheritance
The Object class defined in System namespace is the ultimate base of each class
Interfaces in C# can inherit more than one interface Sealed keyword prevents class from being inherited = final keyword in Java
Access Modifiers
Private-can be accessed within the class
Protected-can be accessed from the containing class & types inherited from them
Public-Anyone can access
Protected internal - anything within the project can access or types inherited from them
Internal-anything within the project can access
cannot apply acces modifiers to namespaces
propertiesBuilt in support for accessing private members of a class
Suppose “name “ is one of the private fields of class
Public string Name{
Get{
return name;}Set{
Name=value;}
}Use method - stu.Name = “Yash” ; correct
stu.name=“Yash”; incorrect Convention – Property must be of same name as field name but with letter as capital letter
delegatesreference to a methods
Similar to function pointer concept in C & C++
Delegates can be made to a signature of method
Three Properties of Delegate
1-signature of method that a delegate points toEg. int Add(int a, int b); int Sub(int c, int d); Here both methods have same signatureSo delegate int Mydelegate(int a, int b);
2- reference of delegate that points to a method
Mydelegate arith;
3-Actual methods reference by the delegatearith = new Mydelegate(Add);
Exception handlingExceptions- They r the runtime errors causing the program to terminate prematurely
EgPublic void Main() {
int d=2;int c=0;
program will terminate unexpectedly
To prevent this Exception Handling is used
MechanismSystem.Exception - Base class for all exceptionsThrow keyword – Methods can throw exception explicitly “Try” and “catch” block should be used . Single try block is not allowed suspected code must be in Try block while measures to handle exception must be in catch block
Exception handlingMechanism (cont.)
finally – code must be executed whether exceptions occur or not Eg. Freeing of resources, closing opened files, etcIf we miss, CLR will handle exception for us