Sie sind auf Seite 1von 38

*****************************************************************************

Important Points To Remember In C#

By: Surya Pratap Singh

*****************************************************************************
*

The Microsoft .NET Framework:

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.

The Microsoft .NET Framework was developed to solve this problem.

.NET Frameworks keywords:

• Easier and quicker programming


• Reduced amount of code
• Declarative programming model
• Richer server control hierarchy with events
• Larger class library
• Better support for development tools

The .NET Framework consists of 3 main parts:

Programming languages:

• C# (Pronounced C sharp)
• Visual Basic (VB .NET)
• J# (Pronounced J sharp)

Server technologies and client technologies:

• ASP .NET (Active Server Pages)


• Windows Forms (Windows desktop solutions)
• Compact Framework (PDA / Mobile solutions)
Development environments:

• Visual Studio .NET (VS .NET)


• Visual Web Developer

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?

C# is a Language and it uses the .net framework or platform.

Table indicating the year and the different versions of Visual Studio:

Year Versions Failure/Success


200 1.1 Success
2
200 2.0 Success
5
200 3.0 Failure
7
200 3.5(called as winfx or orcus) Success
8

BCL(Base Class Libraries):

It contains predefined functions. 14,000 classes are present in BCL.

MSIL(Microsoft Intermediate Languages):

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++.

IDE(Integrated Development Environment)

In the IDE we write and execute the code. The output we get in the output window.

CLR(Common Language Runtime)


It has two parts:

a) Compile_time: It contains different namespaces.

b) Run_time: It again consists of two subparts:

1> CTS (Common Type System)

It allocates memory for to the different types of data of different languages.

2> CLS (Common Language Specification)

CLS is a subset of CTS. It converts to single .Net understandable language.

JIT (Just In Time)

It is a compiler that converts the framework understandable code back to native code.

Framework Life Cycle:

IDE Extensions Converter

C#, VB.Net, Etc .CS, .VBC, Etc. MSIL

CLR
Compile time
Using, System, Threading Etc.

CTS CLS Runtime

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.

3> System.Console.ReadLine() & System.Console.WriteLine(): These functions have


same meaning as Console.ReadLine() & Console.WriteLine() respectively but they are
used when the system namespaces are not included.

Sample Program:

Using System;

Using System.Collections.Generic; //These are the predefined namespaces

Using System.Text;

namespace ConsoleApplication1 //ConsoleApplication1 is the project name

class Program //This is the program name

static void Main(string[] args)


{
int a, b, c;
Console.Write("Enter the first no:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second no:");
b = int.Parse(Console.ReadLine());
c=a+b;
Console.WriteLine("The sum is:"+c); //Here + is the concatenate operator
Console.Read(); //This statement act as getch() in C.

In .Net there are 3 types of applications:


1) Console Applications

2) Windows Applications

3) Web Applications

• Console Applications: The output appears in the command prompt window.

Where it is used?

 It is used in game kind of projects.

• Windows Applications: It is also called as winforms or it is used for desktop


applications. Here the output is form based i.e. drag and drop option is present.

Where it is used?

 We can develop intranet(LAN) projects.

• Web Applications: The execution is done in the browser.

Where it is used?

 We can develop internet projects.

For Converting a Datatype into String:

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:

For example: a=datatype.Parse(Console.ReadLine());

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

Dim a,b,c As Integer //Declaration of a variable

a=Console.ReadLine() //The basic i/o functions are same

b=Console.WriteLine()

c=a+b

Console.WriteLine(“The output is”&c) //& is the concatenate operator

Console.Read()

End Sub

End Module

*****************************************************************************
*

*****************************************************************************
*

Looping & Control Statements: (They are ll’ar to the C Language)

1> While & Nested While loop

2> Do-while & Nested Do-While loop

3> Continue statement

4> Jump

5> Goto

6> Switch-Case & Nested Switch-Case

7> For & Nested For

8> If & Nested If


9> If-else & Nested If-else

Types Of Arrays in C#.Net:

1> Ordinary Arrays:

syntax for 1-D array:

datatype[] variable_name=new datatype[size of array];

Example:

static void Main(string[] args)


{

int [] arr=new int[5]{2,3,7,8,9};

for(int i=0;i<=4;i++)

Console.WriteLine(arr[i]);

Console.Read();

syntax for 2-D array:

datatype[,] variable_name=new datatype[row_size, column_size];

Example:

static void Main(string[] args)


{
int[,] arr = new int[3, 2] { {14,23},{90,77},{45,5}};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine(arr[i,j]);
}
}
Console.Read();
}

syntax for 3-D array:


datatype[,,] variable_name=new datatype[no_of_times, row_size, column_size];

Example:

static void Main(string[] args)


{
int[,,] arr =new
int[3,2,2]{{{14,23},{90,77}},{{32,54},{65,67}},{{56,334},{6,76}}};
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine(arr[k,i,j]);
}
}
}
Console.Read();
}

2> Jagged Arrays: Here the row size is fixed but the column size can be changed for each
row.

syntax for jagged array:

datatype [][] array_name=new datatype[rowsize][];

array_name[row_no]=new datatype[column_size];

Example:

static void Main(string[] args)


{
int[][] arr = new int[3][];
arr[0] = new int[3] { 67, 89, 90 };
arr[1] = new int[5] { 6, 09, 234, 65, 90 };
arr[2] = new int[2] { 56, 77 };
Console.WriteLine("First Row:");
for (int i = 0; i < arr[0].Length; i++)
{
Console.WriteLine(arr[0][i]);
}

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

Array Bounds Checking In C#:

 IndexOutOfRangeException() exception occurs if the array range exceeds the given value
of the index.

*****************************************************************************
*

*****************************************************************************
*

Memory Management:

Value Type Reference Type


Stack Memory Used Heap Memory Used or Allocated
Execution Fast Execution Slow
Ex: int a=10; Ex: Objects, Constructors, Array, ArrayList Etc.

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.

Syntax for for-each loop:

Foreach(datatype or object varaible in object_name for the arraylist);

Example for Array List:

static void Main(string[] args)

{
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:

static void Main(string[] args)


{
Stack s = new Stack();
s.Push("Surya");
s.Push("Loves");
s.Push("Tanuja");
s.Pop();
s.Pop();
Console.WriteLine(s.Pop());

Queues: It based on the principle of First-In-First-Out(FIFO) or Last-In-Last-Out(LILO). Here


“object.Enqueue” is used to insert the values in the queue and to retrieve the values
“object.Dequeue” is used.

Example:

static void Main(string[] args)


{
Queue q = new Queue();
q.Enqueue("I");
q.Enqueue("Love");
q.Enqueue("My");
q.Enqueue("Country");
Console.WriteLine(q.Dequeue());
Console.WriteLine(q.Dequeue());
Console.WriteLine(q.Dequeue());
Console.WriteLine(q.Dequeue());

*****************************************************************************
*

*****************************************************************************
*

Difference between ASP & ASP.net:

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.

Difference between ADO & ADO.net:

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.

Partial support for OOPs. Purely object oriented.


Data access very slow. Fast data access.
Supports only COM (component object model). Supports both COM & Assemblies.

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.

Assemblies what they contain:

Assemblies contain information:

1> Meta data: information about the different datatypes.

2> Manifest: It contains info such as version info.

3> IL code: intermediate language code.


Note: we can see this info in the visual studio cmd prompt by typing in ILDASM (intermediate
language de-assembler).

Procedure to create an assembly:

Step 1> first go to file->new->project->class library give the name as required.

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.

Demerits of the DNA (Distributed Network Architecture):

1> It is only upto 3 tier.

2> For languages like C,C++ (use of pointers is a difficult & no rapid appln’s ).

For VB (it does not supports OOPs fully).

For ASP (it is an interpreted language, only supports client scripting lang’s, again &
again we have to restart the server).

For ADO (partial support for XML, purely connected architecture).

Component Object Model (COM):

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:

In C# we can assign the values directly to the variables.

There are 5 access specifiers in C#:

1> Public

2> Private

3> Protected: same member functions of the class as well as the inherited classes also.

4> Internal: within the same assembly.

5> Protected internal: same assembly as well as inherited assembly.

Classes & Objects:

We can create a class under the namespace or we can create a class within a class.

Example of class under the namespace:

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:

 Object first letter will always be a character.

 (.) is the concatenate operator.

 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:

 There is no return type.


 Whenever we are initializing the object the constructor gets called automatically.

 Class name & method name same always.

 Access specifier (private by default).

Types of constructors:

1> Default Constructor:

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

2> Parameterized Constructor:

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.

 Preceded by the tile (~) symbol.

 No need of return type.

 No need of access specifiers.

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

1> Single Inheritance

2> Multilevel Inheritance

3> Multiple Inheritance is not supported by .net instead of this we have “interfaces”

4> Hybrid Inheritance

5> Hierarchal Inheritance

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.

Example of method overloading:

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

static void Main(string[] args)


{
a obj = new a();
obj.poly();
obj.poly(20);
obj.poly(45.78);
obj.poly("tanuja");
Console.Read();
}
}

Difference between Polymorphism & Constructor:

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:

This is used inplace of multiple inheritance.

• By default it is internal that is we can use it within an assembly.

• Only method delcaration not for defination.

• 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");
}

static void Main(string[] args)


{
c obj = new c();
obj.fun();
obj.fun1();
obj.fun2();
obj.fun3();
Console.Read();
}
}

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.

• Virtual keyword is used infront of the method in the base class.


• To override that method override keyword is used in the derived class.

Example of function overriding:

class shape
{

public virtual void shapetype()


{
Console.Write("this method");
}

public int area(int side)


{
return (side * side);
}
public double area(double breadth, double height)
{
return (0.5 * breadth * height);
}

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:

1> Ordinary Classes: inherit or without inheritance.

2> Sealed Classes: we cannot inherit.

3> Abstract Classes: we should inherit & call.


Sealed Classes:

• It is used to prevent classes from being inherited.

• We can’t inherit the sealed class.

• Always we have to create an instance directly.

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:

• We should always inherit and call.

• Its having abstract and non-abstract methods.

• In derived class override those abstract methods.

• Non-abstract methods contains defination as well as declaration.

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;
}
}

static void Main(string[] args)


{
b obj = new b();
Console.WriteLine("Value is" + obj.cal());
Console.Read();
}

*****************************************************************************
*

*****************************************************************************
*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;
}
}
}

static void Main(string[] args)


{
a obj = new a();
obj[5] = 100;
obj[8] = 300;
for (int i = 0; i <= 9; i++)
{
Console.WriteLine(obj[i]);
}
Console.Read();
}
}

*****************************************************************************
*
*****************************************************************************
*
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;

case 2: Console.ForegroundColor = ConsoleColor.Cyan;


Console.Write("\nthe data in the file:\n");
StreamReader sr = new StreamReader(@"f:\tanuja");
while ((str = sr.ReadLine()) != null)
{
Console.WriteLine(str);
}
break;

case 3: FileStream fs = new


FileStream(@"f:\tanuja",FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("\nenter some data: ");
str = Console.ReadLine();
sw.WriteLine(str);
sw.Close();
fs.Close();
Console.WriteLine("\ndata appended.");
break;

case 4: FileInfo fi = new FileInfo(@"f:\tanuja");


Console.WriteLine("\ndelete the file y/n: ");
string ch;
ch = Console.ReadLine();
if (ch != "N")
{
fi.Delete();
Console.WriteLine("\nfile deleted.");
}
break;
default: Console.WriteLine("\ninvalid choice.");
break;
}
Console.Read();
}
Delegates:

• Function pointer

• Call back function


Note 1`: In .net we don’t have any pointer concepts instead of that we are using delegates.
Note 2: For different return types and signatures different delegate objects are required.
Deleagtes: it is an object which can hold the address of n no of methods.
Types of delegates:

• 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

del d = new del(obj.fun); //binding the function


with the delegate
d += new del(obj.fun2);
del1 d1 = new del1(obj.fun1);
d1(100);
d(); //calling the delegate

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

 Only supports client side scripting languages

 Again again we have to restart the server

ADO: Activex Data Object

 Purely connected architecture

 Partial support for xml


Xml: used for data transfer
(serialization: conversion of object to stream type & deserialization: conversion of steam
to object type takes place)

• 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.

 N-tier Archictecture with assemblies


Assemblies:

 Building blocks of the program

 Logical units of data

 They contain inforamtion about the assemblies:


(a) Metadata //mathod name,class,datatypes etc
(b) Manifest //version info
(c) Il(intermediate language) code
Types of assemblies:
1. Private Assembly: we cannot use in future only for the current project.
(a)satellite assembly: to maintain the culture languages. To convert from one language
to another.

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:

 If we make a class as static then we cannot make an instance of that class.


Difference between html & xml:

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:

.Net 1.1 .Net 2.0


By default only one namespace By default 3 namespaces
New Features:
 In C#
(a) Generics
(b) Partial Classes
(c) Nullable types
(d) Delegates
 In Asp.Net & Winforms
(a) Tabular data bound controls
(b) Navigation controls
(c) Html comtrols
(d) Ajax extensions

*****************************************************************************
*
*****************************************************************************
*
Generics:

• By using generics we can access multiple datatypes.

1. Generics with properties:


public class aa<t> //t is the generic name it can be any letter uppercase or lowercase
{
t a; //a is the local variable for the generic class
public t hai
{
get
{
return a;
}
set
{
a = value;
}
}
}

static void Main(string[] args)


{
aa<int> obj = new aa<int>();
obj.hai=100;
Console.Write(obj.hai);
aa<string> obj1 = new aa<string>();
obj1.hai = "\a\nWelcome To Generics";
Console.Write(obj1.hai);
Console.Read();
}
}
2. Generics with Method Overloading:
class aa<t,u> //we can declare as many as we want generic variables seperated by the
comma operator
{
t a;
u b;
public void hai(t a1)
{
a = a1;
}

public void hello(u b1)


{
b = b1;
}
public void display()
{
Console.Write(a+"\t"+b);
}
}

static void Main(string[] args)


{
aa<int, string> obj = new aa<int, string>();
obj.hai(700);
obj.hello("Surya");
obj.display();
aa<int, int> obj1 = new aa<int, int>();
Console.Write("\n");
obj1.hello(200);
obj1.hai(20);
obj1.display();
Console.Read();
}
3. Generics with Delegates:
delegate u deel<u>(u hai);
//syntax: delegate returntype_or_generic_name delegatename<name of
generic>(generics local varibales separated by comma);
class a
{
public int wel(int a)
{
Console.Write("\ninteger delegate: " + a);
return 0;
}
public int we(int a)
{
Console.Write("\n\nMulticast Deleagte: " + a);
return 0;
}
public string wel1(string a)
{
Console.Write("string delegate: " + a);
return a;
}
public double fun(double x)
{
Console.Write("double: " + x);
return 0;
}
}

static void Main(string[] args)


{
a obj=new a ();
deel <int> dd=new deel<int> (obj.wel);
dd+=new deel<int> (obj.we);
Console.Write("\n");
dd(100);
Console.Write("\n\n");
deel<string > dd1=new deel<string> (obj.wel1);
dd1("sir");
Console.Write("\n\n");
deel<double> ddd=new deel<double> (obj.fun);
ddd(109.67676);
Console.Read();
}
*****************************************************************************
*
*****************************************************************************
*
Itterators:
1. Simple Itterators: Simple itterators for single data types.

 We have to include the namespace “System.Collections”.


Example:
using System.Collections;
class hai
{
string[] name = { "101", "102", "100" };
string[] desig = { "software", "consultant", "manager" };
public IEnumerator GetEnumerator()
{
for (int i = 0; i < name.Length; i++)
{
yield return name[i];
yield return desig[i];
}
}
}
static void Main(string[] args)
{
hai obj = new hai();
foreach (string str in obj)
{
Console.Write(str);
Console.Write("\n");
}
Console.Read();
}
2. Itterators with Generics:
Example:

class hai<t>
{
t[] item;
public hai(t[] val)
{
item=val;
}
public IEnumerator<t>GetEnumerator()
{
foreach(t value in item)
{
yield return value ;
}
}
}

static void Main(string[] args)


{
string[] department = { "marketing", "finance", "information" };
hai<string> obj = new hai<string>(department);
foreach (string val in obj)
{
Console.Write(val + "\n");
}
Console.Read();
}
Nullable Types:

• They are used to assign null value to a variable.

Example:
static void Main(string[] args)
{
int? x = null;
if (x.HasValue == true)
{
Console.Write("Hai");
}
else
{
Console.Write("False");
}
Console.Read();
}

Das könnte Ihnen auch gefallen