Beruflich Dokumente
Kultur Dokumente
*****************************************************************************
*
The .NET Framework is the infrastructure for the Microsoft .NET platform.
The .NET Framework is an environment for building, deploying, and running Web applications
and Web Services.
Microsoft's first server technology ASP (Active Server Pages), was a powerful and flexible
"programming language". But it was too code oriented. It was not an application framework and
not an enterprise development tool.
Programming languages:
• C# (Pronounced C sharp)
• Visual Basic (VB .NET)
• J# (Pronounced J sharp)
What is .Net?
. Net is a platform (compare to the railway station platform trains come from different sources to
a particular station) or framework. In .Net also different languages can be written and executed.
It supports 54+ Languages such as C#, J#.Net, VB.Net, COBOL.Net, PERL.Net, Python.Net,
XML.Net, ADO.Net etc. .Net is said to be the next generation of windows services.
What is C#.Net?
Table indicating the year and the different versions of Visual Studio:
This is used to convert the source code into framework understandable code.
Namespaces:
It is a collection of classes. In .Net we are having namespaces we don’t have header files as in C,
C++.
In the IDE we write and execute the code. The output we get in the output window.
It is a compiler that converts the framework understandable code back to native code.
CLR
Compile time
Using, System, Threading Etc.
JIT
Native Code
Code
Output
Basic Input & Output Functions in C#:
1> Console.WriteLine(): This is used for printing anything on the output window.
2> Console.ReadLine(): This is used to read a value from the keyboard or to take user
input.
Sample Program:
Using System;
Using System.Text;
2) Windows Applications
3) Web Applications
Where it is used?
Where it is used?
Where it is used?
As we know that the argument we are taking is of string type so we have to convert each and
every datatype to string in each program, if we are entering the values from the keyboard by
using the function:
a=Convert.ToInt32(Console.ReadLine());
InVB.Net:
The statements don’t end with a semicolon in VB.Net. Here no conversion is required as in
C#.Net.
Sample Program:
Module Module1
Sub Main()
b=Console.WriteLine()
c=a+b
Console.Read()
End Sub
End Module
*****************************************************************************
*
*****************************************************************************
*
4> Jump
5> Goto
Example:
for(int i=0;i<=4;i++)
Console.WriteLine(arr[i]);
Console.Read();
Example:
Example:
2> Jagged Arrays: Here the row size is fixed but the column size can be changed for each
row.
array_name[row_no]=new datatype[column_size];
Example:
Console.WriteLine("\nSecond Row:");
for (int j = 0; j < arr[1].Length; j++)
{
Console.WriteLine(arr[1][j]);
}
Console.WriteLine("\nThird Row:");
for (int k = 0; k < arr[2].Length; k++)
{
Console.WriteLine(arr[2][k]);
}
Console.Read();
}
IndexOutOfRangeException() exception occurs if the array range exceeds the given value
of the index.
*****************************************************************************
*
*****************************************************************************
*
Memory Management:
CLR: It has automatic garbage collector. A method named System.Gc.Collect is used for
unwanted memory or garbage collection.
Namespace System.Collection:
Properties present in it are:
1> ArrayList
2> Stacks
3> Queues
4> HashTable
5> SortedList
Array List:
Multiple Datatypes can be added into the array list dynamically. For accessing the values in the
array list “for each” loop is used. If only one variable is there then we use the datatype name in
the for-each loop else we use “object” keyword.
{
ArrayList a = new ArrayList();
a.Add(100);
a.Add(34.67);
a.Add("Surya");
foreach (object i in a)
{
Console.WriteLine(i); }}
Stacks: Here “object.Push” is used to insert the values and “object.Pop” is used to retrieve or
delete the values. It is based on the principle of First-In-Last-Out(FILO) or Last-In-First-
Out(LIFO).
Example:
Example:
*****************************************************************************
*
*****************************************************************************
*
ASP ASP.net
The extension here is .asp The extension here is .aspx
It is an interpreted language It is a compiled language
For running this we require browser (internet explorer)
and server (IIS-internet information services)
ADO.net: This stands for active x data object. It is used for the connection with the db.
ADO ADO.net
Purely connected architecture i.e. only Connected & disconnected architecture.
when the frontend and backend connection
is opened the data transfer can take place.
Partial or Limited support for XML i.e. Full support for XML.
multiple records cannot be inserted at a time.
Why XML?
XML is used to convert the different database queries in a standard format also called as
serialization or we can call conversion of object to stream type. Similarly de-serialization takes
place or conversion of stream to object type.
Assemblies: They are the logical units of data or building blocks of programs or collection of
DLL(dynamic link library) & exe file.
Types Of Assemblies:
1> Private Assemblies: That is used only for the current application. Once we close the
visual studio the assembly cannot be used again.
2> Shared Assemblies: It can be used for future use i.e. it can be used in any project
whenever required. For this it has to be registered in the GACHE in .net folder.
Step 2> then write the set of classes and methods u want & then click on build. As we click on
build we get the dll & the exe file.
Step 3> then we can use it in any other application by right clicking on the name of the
application in the solution explorer and then go to ADD reference->application name->bin-
>debug there we will get a .dll file that we have to add and then we have to include that assembly
in our required project by writing using ournamespace.
2> For languages like C,C++ (use of pointers is a difficult & no rapid appln’s ).
For ASP (it is an interpreted language, only supports client scripting lang’s, again &
again we have to restart the server).
This is used to create the business logic & the COM can be called in other languages to access
the business logic.
Demerit:
For adding the business logic the COM doesn’t adds it replaces the new code with the old code.
Note:
Because of all these problems .Net (next generation of windows services) was introduced
which is supports n-tier architecture and it has assemblies back compatibility of version
programming is not possible.
*****************************************************************************
*
*****************************************************************************
*Access Specifiers: It is required to use the variables in the various functions.
Note:
1> Public
2> Private
3> Protected: same member functions of the class as well as the inherited classes also.
We can create a class under the namespace or we can create a class within a class.
using System;
using System.Collections.Generic;
using System.Text;
namespace console_prgs
{
class a
{
public void fun()
{
Console.WriteLine("Classes & Objects");
}
}
class Program
{
static void Main(string[] args)
{
a f1 = new a();
f1.fun();
Console.Read();
}
}
}
Example of class within a class:
using System;
using System.Collections.Generic;
using System.Text;
namespace console_prgs
{
class Program
{
class a
{
public void fun()
{
Console.WriteLine("Class within a class");
}
}
static void Main(string[] args)
{
a f1 = new a();
f1.fun();
Console.Read();
}
}
}
Note:
For void functions we don’t have to write the return statement else it will give an error.
For any other functions other than void we should write the return statements else it will
show an error.
*****************************************************************************
*Dated: 23-02-2009
*****************************************************************************
*Constructor:
Types of constructors:
Example:
using System;
using System.Collections.Generic;
using System.Text;
namespace console_prgs
{
class Program
{
class a
{
public a()
{
Console.WriteLine("default");
}
}
static void Main(string[] args)
{
a f1 = new a();
Console.Read();
}
}
}
Example:
using System;
using System.Collections.Generic;
using System.Text;
namespace console_prgs
{
class Program
{
class a
{
public a(int x)
{
Console.WriteLine("Parametrized Constructor:"+x);
}
}
static void Main(string[] args)
{
a f1 = new a(7);
Console.Read();
}
}
}
Destructor: If we want to release the memory then and there then we can go for destructor.
Example:
using System;
using System.Collections.Generic;
using System.Text;
namespace console_prgs
{
class Program
{
class a
{
public a(int x)
{
Console.WriteLine("Parametrized Constructor:"+x);
}
~a()
{
Console.WriteLine("the memory released");
}
}
static void Main(string[] args)
{
a f1 = new a(7);
Console.Read();
}
}
}
Inheritance
3> Multiple Inheritance is not supported by .net instead of this we have “interfaces”
Polymorphism
1> Compile Time: Early binding techniques such as: method or function overloading,
operator overloading.
2> Run Time: Late binding techniques such as: function overriding.
class a
{
public void poly()
{
Console.WriteLine("Polymorhism");
}
public void poly(int a)
{
Console.WriteLine("integer" + a);
}
public void poly(string a)
{
Console.WriteLine("string" + a);
}
public void poly(double a)
{
Console.WriteLine("Double" + a);
}
1> In the constructor there is no need of (.) operator directly we can assign the value.
2> In the constructor method name and class name is the same but it is not so in
Polymorphism.
3> Different objects should be used for different constructors but in polymorphism only once
we have to declare the object of the class and then we can use it to assign the values for
different methods.
*****************************************************************************
*
*****************************************************************************
*
Interface:
• If you declare the method we should define it in the derived class with the return type &
method name.
Example:
interface a
{
void fun1();
void fun();
}
interface b
{
void fun2();
void fun3();
}
class c : a, b
{
public void fun()
{
Console.WriteLine("interface 1 1st nethod");
}
public void fun1()
{
Console.WriteLine("interface 1 2nd method");
}
public void fun2()
{
Console.WriteLine("interface 2 method 1");
}
public void fun3()
{
Console.WriteLine("interface 2 mthod 2");
}
Note: Interface members cannot have defination inside the base class.
Runtime Polymorphism: if we declare a method with the same name and same arguments then
the method in the base class is called but if we want to call the derived class method then we
have to override the method in the base class.
class shape
{
class square:shape
{
public override void shapetype()
{
Console.Write("a square is a polygon");
}
}
static void Main(string[] args)
{
square obj=new square();
obj.shapetype();
shape obj1=new shape();
Console.Write(" area is"+obj.area(2));
Console.Read();
}
Types Of Classes:
Example:
sealed class a
{
public void fun()
{
Console.Write("Sealed Class Create An Instance Directly");
}
}
class b
{
public void fun1()
{
Console.Write("derived class");
}
}
static void Main(string[] args)
{
a obj = new a();
obj.fun();
Console.Read();
}
Abstract class:
Example:
abstract class a
{
public void fun1()
{
Console.Write("non abstract method");
}
public abstract int cal();
}
class b:a
{
public override int cal()
{
int a, b, c;
a = 10; b = 20;
c = a + b;
return c;
}
}
*****************************************************************************
*
*****************************************************************************
*Properties:
• Read & write functions. It is a type of function that doesn’t have any arguments.
Syntax:
return_type property_name
{
get //accessor
{
return //returns a value
}
set //accessor
{
value //assigns a value
}
}
Example:
class a
{
public int x = 100;
public int s_prop
{
get
{
return x; //reads the value that is assigned
}
set
{
x = value; //without set also it will work but we will not be able to assign new value
to the variable
}
}
}
static void Main(string[] args)
{
a ob=new a();
ob.s_prop=200;
Console.WriteLine(ob.s_prop);
Console.Read();
}
Array Indexes: Array indexes can only be used with properties. Here we can assign any value to
any location directly no need for continous memory allocation.
Example: a[10]=15;
Example:
class Program
{
class a
{
int[] arr = new int[10];
public int this[int index] //this is the keyword
{
get
{
return arr[index];
}
set
{
arr[index] = value;
}
}
}
*****************************************************************************
*
*****************************************************************************
*
Exception Handling:
Syntax:
try {
program blocks
}
catch(exception ex) //exception is the class & ex is the object
{
Console.WriteLine(ex.message);
}
Finally
{
File close //this block is always executed
}
Example:
static void Main(string[] args)
{
try
{
int[] arr = new int[5] { 10, 20, 30, 40, 50 };
for (int i = 0; i <= 10; i++)
{
Console.Write(arr[i]);
}
}
catch (Exception ex)
{
Console.Write("\nerror:" + ex.Message);
}
finally
{
Console.Write("\nException Handling");
}
Console.Read();
}
File I/O:
We have to include the namespace using system.IO for getting the system i/o functions such as :
1> Streamwriter:- creating a file
2> Stream reader:- for reading the contents of a file
3> Stream:– for adding the data of a file
Example:
static void Main(string[] args)
{
string str;
int choice;
Console.Write("1.write\n2.read\n3.Append\n4.deletefile");
Console.Write("\n\nEnter ur choice: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1: Console.Write("\nenter data: ");
str = Console.ReadLine();
StreamWriter ss = new StreamWriter(@"f:\tanuja");
ss.WriteLine(str);
ss.Close();
Console.Write("\nwe have a new file");
break;
• Function pointer
• Single delegates
• Multicast delegates
Syntax:
access_specifier delegate return_type delegate_name();
Example of single delegates:
public delegate void del();
class a
{
public void fun()
{
Console.Write("delegates");
}
}
static void Main(string[] args)
{
a obj = new a(); //object to the functions of class a
del d = new del(obj.fun); //binding the function with
the delegate
d(); //calling the delegate
Console.Read();
Console.Read();
}
Example of multicast delegates:
public delegate void del();
public delegate int del1(int c);
class a
{
public void fun()
{
Console.Write("\ndelegates1");
}
public int fun1(int x)
{
Console.Write("\nDelegate "+x);
return x;
}
public void fun2()
{
Console.Write("\nSurya");
}
}
static void Main(string[] args)
{
a obj = new a(); //object to the functions of class a
Console.Read();
Console.Read();
}
Note: Pointers can be used but within the unsafe part.
*****************************************************************************
*
*****************************************************************************
*
Component Object Model (COM):
1> Presentation layer //asp.net,C#.net,vb.net
2> Appllication layer or business logic //the actual code
3> Data access layer //retrieving info from the db
Asp: Active server pages
It is interpreted languages
• Com faced the “DLL hell problem” that is the code once debugged could not be modified
it could only be replaced with the previous code. Repeated task could not be performed.
Com Marshalling: we can convert to the languages we require the code by using com
marshalling.
.Net: It is called the next genration of windows services.
2. Shared Assembly: we can use for the future also for that we have to register it into the
GACache(global assembly cache).
How register assembly in the GAC:
Step 1: Open a new project->class library.
Step 2: write the functions and then build it don’t debug.
Step 3: go to start->all program->visual studio 2008->visual studio tools->vsiual studio cmd
prompt.
Step 4: go to the drive u want to save the strong name and then type: “sn –k
userdefined_name.snk”.
Step 5: then go to the properties of the class library and select option signing in that go to
signing browse for the strong name file and then rebuild the solution.
Step 6: then again go to visual studio cmd prompt and then type:”gacutil –i
dllfile_of_class_library”. Then it will show a message that class library is added to the
GACache.
Note: To see ur class library u can go to “c:\windows\assembly” there in the list of assemblies u
will find ur class library file.
*****************************************************************************
*
*****************************************************************************
*
Static Class:
Html XML
Hyper text markup language Extensible markup language
Predefined tags Some predefined tags & we can define our
own userdefined tags
Interpreted language Compiled & interpreted Language
It is used only for presentation Used for data tranfer (serialization &
deserialization of data)
XML:
We have to include the namespace: “using System.XML”;
*****************************************************************************
*
*****************************************************************************
*
Overflow exceptions:
If we exceed the datatype range we get an overflow exception to avoid the exception we
can go with checked & unchecked.
Checked Example:
class Program
{
static void Main(string[] args)
{
byte a = 255;
byte b = 1;
try
{
checked
{
byte result1 = (byte)(a+b);
Console.WriteLine("Result is:" + result1);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("*********************");
Console.Read();
}
}
}
Unchecked example: if we go with unchecked then we will not get get the exception message
instead we will get a value =0.
class Program
{
static void Main(string[] args)
{
byte a = 255;
byte b = 1;
try
{
unchecked
{
byte result1 = (byte)(a+b);
Console.WriteLine("Result is:" + result1);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("*********************");
Console.Read();
}
}
}
Managed Code: It is handled by CLR.
Unmanagd Code: It is not handled by CLR we go with unsafe keyword. For example if we want
to use pointers that is not allowed in C# we can go with “Unsafe Keyword”.
Difference between .Net 1.1 & .Net 2.0:
*****************************************************************************
*
*****************************************************************************
*
Generics:
class hai<t>
{
t[] item;
public hai(t[] val)
{
item=val;
}
public IEnumerator<t>GetEnumerator()
{
foreach(t value in item)
{
yield return value ;
}
}
}
Example:
static void Main(string[] args)
{
int? x = null;
if (x.HasValue == true)
{
Console.Write("Hai");
}
else
{
Console.Write("False");
}
Console.Read();
}