Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction
Microsoft Corporation
It is developed MS.Net
.Net is not a open source. We need
to pay money to use this software.
.net
having
rich
graphic
user
interface editor.
VS2008
3.0 & 3.5
Vs2010
4.0
VS2012
4.5
Note: Each .net framework language having the facility to develop windows and Web applications
Web
Win
Before the MS.Net which technologies used for Windows and Web:
a) VB 6.0(Visual Basic) for windows Application
b) ASP 3.0(Active Server Pages) for web Application
Active Server Pages (ASP) 3.0:
It is a combination of Html+ JavaScript + VBScript.
HTML Used for web page development
JavaScript used for client-side validations. so we called JavaScript is Clint-side script.
VBScript is used for server-side Transactions. Mainly used data base transactions.
Java
Java Data types are not an objects
Java does not support.
In Java Parameters passed by values
Public:
Public members have no access restriction. We can access public members by creating object, in
derived class and with in the assembly and in other assemblies also.(in another application). . It can
be accessed everywhere.
Field , Properties and Members declared with public keyword are accessible from anywhere in the
class and outside of the class also we can access.
Within the class - Visible
a)Visible the public members within the same Class
b) Visible the public members in other Class by creating the object
c)Visible public members in derived/inherited/child Class
4)Visible public members from other namespace(application) by creating the object.
5)Visible public members from other namespace(application) by inheritance.
Private:
The scope of the private members is limited.
They are not accessible outside the Class or structure , block or module .
We cannot access private members by creating object and also we cannot access private members in
derived class.
Private keyword allows a class to hide its member and variables from other class objects and
functions. So it is not visible outside the class.
When you do not specify any data member as public, protected, or private, then the default access
specifies for a data member is private.
a)Visible the private members within the same Class
b) We cannot visible the private members in other Class by creating the object.
c) We cannot visible private members in derived/inherited/Child Class.
d)We cannot visible private members from other application by creating the object.
e) We cannot visible private members from other application by inheritance.
Protected:
You cannot access protected members by creating object. You can access protected members only
in derived/inherited/child class.
The type or member can only be accessed by code in the same class or struct, or in a derived class.
a)Visible the protected members within the Same Class
b)We cannot visible the protected members in other Class by creating the object.
c)We can visible protected members in derived/inherited/Child Class.
c)We cannot visible protected members from other application by creating the object.
d)We can visible protected members from other application by the inheritance.
Note: Variable declared with protected keyword are accessible within the class and inherited
classes.
The protected access specified in C# allows a class to hide its variables and member from other
class objects except the child class. This
Field , Properties and Members declared with Protected keyword are accessible in the inherited
classes
Internal:
Internal members can be accessed from any class or method defined within the application but not
from another application.
If we do not specify any data member as public, protected, or private the default access specifier
for a class is internal.
a)Visible the internal members within the same Class
b)We can visible the internal members in other Class by creating the object.
c)We can visible internal members in derived/inherited/Child Class.
d)We cannot visible internal members from other application by creating the object
e)We cannot visible internal members from other application by inheritance.
Protected internal:
The scope of this pecifier is similar to the protected as well internal.
access the member within the class and from other application we able to get it through the
inheritance.
a)Visible the protected internal members within the same Class
b)Can visible the protected internal members in other Class by creating the object.
c)Can not visible protected internal members in derived/inherited/Child Class.
d)We cannot visible protected internal members from other application by creating the object.
d)We can visible protected internal members from other application by inheritance.
Destructors cannot have accessibility modifiers.
The type or member can be accessed by any code in the same assembly, or by any derived class in another
assembly
Value Type: Value data type variables contain the data directly.
Value types are stored in the stock memory.
Reference Types:
The reference type used to store the address location of the data. reference of another memory
location that contain the data.
Value Type
Value types are stored on the
stack
A value type variable contain the
data directly.
Value types cannot be inherited
// C#
bool b = false
Reference Type
Reference types stored on the managed
heap.
The reference type used to store the
address location of the data
reference types can inherited.
Parameters:
Difference between the Ref and Out keywords:
Ref:
1. Ref is a keyword. It keyword used while passing and receiving the variables.
2.It allows to modify the original variable.
Ref
Variable must be initialized
public class A
{
public int Fun(ref int x)
{
return 2 * x;
}
}
Calling:
A obj = new A();
int x;
x = 6;//Variable need to be initialized
obj.Fun(ref x);
Out
Initialization of the variable is
optional.
public class A
{
public void Fun(out int x)
{
x= 2 * 5;
}
}
Calling:
A obj = new A();
int x;//Variable initialization should be
optional.
obj.Fun(out x);
Out:
1.Out is a keyword. Initialization of the out variable optional.
The enum declaration ends with the enumerator list. The enumerator list contains the constant
assignments for the enumeration, each separated by comma.
The enumeration begins at 0 and each subsequent value count up from the previous
Example:
public class Default2 : System.Web.UI.Page
{
int first, second, third, fourth;
public enum Count
{
first,
second,
third=20,
fourth
};
protected void Page_Load(object sender, EventArgs e)
{
Response. Write((int)Count.first);
Response. Write((int)Count.fourth);
}
}
Output:
0
21(the value of 3rd will be 20 and fourth will be 21).
The enumeration begins with 0 and each subsequence value count up from the previous so fourth
value is 21
Note: the enumerator cannot declare inside the procedure. it can declare at class or module.
Constants:
The variables whose values do not change during the execution of a program are known as constants
ex: const int rows =10;
Constant
Constant must be initialized at compile time.
public const int x=2;
1.size is fixed
2.Allows only similar type of data.
Single dimensional
Jagged arrays
Single-Dimension arrays:
Array have only in on dimension
Declaration of single dimension array:
int[] x = new int[5];
int[] x1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] x2 = { 1, 2, 3, 4, 5 };
string[] WeekDays = new string[] {"Sun","Mon","Tues","Wed","Thu","Fri","Sat" };
Example:
Output:
1,2,3,4,5
Multi-Dimension arrays:
Arrays can have more than one dimensions.
Declaration of Multi dimension array:
Example:
protected void Button1_Click(object sender, EventArgs e)
{
int[,] x = new int[,] {{1, 2},{3, 4},{5,6},{7,8} };
for(int i=0;i<3;i++) //0 to 3 is no of rows=4
{
for (int j = 0; j < 1; j++) //0 to 1 is no of columns=2
{
Response.Write(x[i,j]);
}
}
Output:
1,2,3,4,5
Jagged-Dimension arrays:
jagged array is an array whose elements are arrays.
Jagged array is sometimes called an array-of-arrays and also called dynamic arrays.
It is called jagged because each of the rows need not be the same size as all the others and thus a
graphical representation of the array would not be square.
Declaration of single dimension of Jagged array with 3 elements:
int[][] MyJaggedArray = new int[3][];
int[][] MyJaggedArray1 = new int[][] { new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3, 4},
new int[] { 1, 2}
};
int[][] MyJaggedArray2 = {
new int[] { 1, 2, 3, 4, 5 },
new int[] { 1, 2, 3, 4},
new int[] { 1, 2}
};
Array Conversion:
Array conversion two types:
Delegate:
A delegate in C# is similar to a function pointer in C or C++ but A Delegate is a type safe function
pointer that contains the details of a method(reference of the method) rather than the data.
Purpose of delegates in C#.Net:
a) Delegates can be used to define callback methods
b) Event Handling
c) improves the performance of application
Abstracting and encapsulating methods
1. Asynchronous programming
2. Sequential programming etc.
Delegates can be chained together; for example, multiple methods can be called on a single event.
Delegate general meaning " a person acting for another person" (i.e in C#.net it is really means a
method acting for another method.
There are four steps in defining and using delegates:
a) Delegate declaration
b) Delegate methods definition
c) Delegate instantiation
d) Delegate invocation.
Delegate is a pointer to a method. A delegate looks and behaves much like are ordinary when it is
called.
Example1:
namespace nNameSpace
{
//Declaration
public delegate void SimpleDelegate();
public class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(TestDelegate.MyFunc);
// Invocation
simpleDelegate();
}
Example2:
namespace MyFirstDelegate
{
//This delegate can point to any method,
//taking two integers and returning an
//integer.
public delegate int MyDelegate(int x, int y);
//This class contains methods that MyDelegate will point to.
public class MyClass
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Multiply(int x, int y)
{
return x * y;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Collections:
collection classes are one type of array class.
collection classes are used for maintaining list of arrays.
Name Space: system.Collection
There are five types of collections in the .net framework
1.ArrayList
2.HashTable
3.SortedList
4.Queue and
5.Stack
What is an Array List?
In array list each item stored in sequential order
Array List contains a simple list of values.
.
Array List implements the IList interface using an array
Array List resizes dynamically.
Example:
/*
Q1.How
Q2.How
Q3.How
Q4.How
Q5.How
to
to
to
to
to
*/
//Create an ArrayList and add three elements
ArrayList Villages = new ArrayList();
Example:
ArrayList Villages = new ArrayList();
Villages.Add("bb");
Villages.Add("cc");
Villages.Add("aa");
ArrayList States = new ArrayList();
States.Add("A.P");
States.Add("U.P");
States.Add("M.P");
States.AddRange(Villages);//explicitly add begining of array list.
for (int i = 0; i < States.Count; i++)
{
string fName = States[i].ToString();
}
Output:
A.P
U.P
M.P
Bb
Cc
Aa
Array
Array is fixed size in its
declaration
Array List
Array list similar to an array but
it does not have a limited size.
increased size dynamically.
Hash Table:
In hash table there is no sequential ordering of elements.
hash table indexes each element with a alphanumeric key.
example:
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
Example1:
static Hashtable GetHashtable()
{
// Create and return new Hashtable.
return hashtable;
Example2:
static Hashtable GetHashtable()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(300, "Carrot");
hashtable.Add("Area", 1000);
return hashtable;
}
Sorted List:
The sorted list class internally maintain two array's
1.A stored array of the key
2.An array of the value.
In sorted list elements(Items) stored based on the alphanumeric key and a numeric index.
Sorted list can be indexed by both key and value.
example:
Example1:
static Hashtable GetHashtable()
{
// Create and return new Hashtable.
SortedList mSortedList = new SortedList();
hashtable.Add("Area", 1000);
hashtable.Add("Perimeter", 55);
hashtable.Add("Mortgage", 540);
}
return hashtable;
Queue:
Arraylist,hashtable and sorted list allows random access to their elements but stock and queue
provide sequential access only.
Queue is the first-in first-out(FIFO) or last-in last-out(LILO) data structure
Stock:
stock is a data structure similar to a Queue. Is supports only sequential access.
Stack uses Last-in First out(LIFO) or First-in Last Out.
Generic Class
List<T>
Dictionary<T,U>
Queue<T>
We can use the SortedList<T,U> generic class with the custom Person.We would use it
with an integer
// C#
SortedList<string, person> sl = new SortedList<string,person>();
sl.Add("One", new person("Mark", "Hanson"));
sl.Add("Two", new person("Kim", "Akers"));
sl.Add("Three", new person("Zsolt", "Ambrus"));
foreach (person p in sl.Values)
{
Response.Write(p.ToString());
}
We can use the SortedList<T,U> generic class with the custom Person.We would use it
with an integer
// C#
SortedList<string, person> sl = new SortedList<string,person>();
sl.Add("One", new person("Mark", "Hanson"));
sl.Add("Two", new person("Kim", "Akers"));
sl.Add("Three", new person("Zsolt", "Ambrus"));
foreach (person p in sl.Values)
{
Response.Write(p.ToString());
}
Ternary operator:
syntax: <condition>? <result if true> : <result if false>
Example:
int x = 10;
int y = 15;
if (x < y)
{
Response.Write("x is less than y");
}
else if (x > y)
{
Response.Write("x is larger than y");
}
else
{
Response.Write("x is equal to y");
}
String m = (x < y) ? "x less than y" : "x greater than y";
IF Statement:
syntax:
if(Boolean expression)
{
statement
}
else if(expression)
{
statement;
}
else
{
statement;
}
Switch statement:
syntax:
Switch(i)
{
Case 0:
Statement;
Break;
Case 1:
Statement;
Goto Default;
Break;
Default:
Statement
Break;
}
Looping:
1. for loops
2.while loops
3.do loop or do while loop
There are some interrupts in the loops:
1.break
2.continue
3.goto
4.return
For loop:
syntax:
For(initializer;condition;iteration)
{
Statements;
}
While loop:
syntax:
while(expression)
{
Statements;
}
do while loop:
syntax:
Do
{
Code to be looped;
While(<text>);
}
Structure:
A structure is simple used-defined value type.
structure is similar to the class. It also contain the fields , Properties , Methods , events and
constructor etc but it can't support the inheritance, destructors and default constructors.
By default all are public in structure. we can't initialize the values in structure like
Private int xVal=50;
Private int yVal=100;
Operators
Operators Name
Arithmetic operators
Logical(Boolean & bit
map)
String operator
Increment, decrement
Shift
Relational
Assignment
Type information
Indirection & Address
Type casting:
Type conversion:
Example:
Operators
+,-,*,/,%
&,/,^,!,~,&&,||,true, false
+
++,-<<,>>
==,!=,<,>,<=,>=
=,+=,-=,*=,!=,&=,%=,^=,<<=,>>=,?
Is,typeof,sizeof
*,->,[],&
Global.asax file:
This file contain global code for the web application.
It contain the even handling code for used to react the application.
Global.aspx page having the following events:
Example:
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to
StateServer
// or SQLServer, the event is not raised.
}
</script>
1.Application_Start: when the application receives the first user request the application
start event is fired.
2.Application_Start: When all the pending requests are completed the application end
event is fired and the application is restarted.
Web.config file:
1.Web.config files used to stored the connection string and global.aspx file also used to store the connection
string.
Example:
<configuration>
<appSettings>
<add key="Conn" value="Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=FinQuest;Data Source=LAXMAN-PC">
</add>
</appSettings>
</configuration>
How to call the connection string in web application:
string conn = "";
conn=System.Configuration.ConfigurationSettings.AppSettings["Conn"].ToString();
Note: we can use more than one web.config files in web applications.
Dispose
String Builder