Vous êtes sur la page 1sur 20

Chapter-1 What Is .NET ?

This chapter gives you an introduction to the .NET technology and explains what is .NET.

.NET is a major technology change for Microsoft and the software world. Just like the computer world moved from DOS to Windows, now they are moving to .NET. But don't be surprised if you find anyone saying that "I do not like .NET and I would stick with the good old COM and C++". There are still lot of people who like to use the bullock-cart instead of the latest Honda car.

The simple answer is 'it is the technology from Microsoft, on which all other Microsoft technologies will be depending on in future.'.

.NET technology was introduced by Microsoft, to catch the market from the SUN's Java. Few years back, Microsoft had only VC++ and VB to compete with Java, but Java was catching the market very fast. With the world depending more and more the Internet/Web and java related tools becoming the best choice for the web applications, Microsoft seemed to be loosing the battle. Thousands of programmers moved to java from VC++ and VB. This was alarming for Microsoft and many of the Microsoft fan's kept on asking "is Microsoft sleeping?". And Microsoft had the answer. One fine morning, they announced : "We are not sleeping. We have the answer for you.". And that answer was .NET.

But Microsoft has a wonderful history of starting late but catching up quickly. This is true in case of .NET too. Microsoft put their best men at work for a secret project called Next Generation Windows Services (NGWS)., under the direct supervision of Mr. Bill Gates. The outcome of the project is what we now know as .NET. Even though .NET has borrowed most of it's ideas from Sun's J2EE, it has really outperformed their competitors.

Microsoft's VC++ was a powerful tool. But it was too complex. It has too many datatypes, and

developers had to learn many libraries including WIndows SDK, MFC, ATL, COM etc. There were many datatype compatibility issues while exchanging data between different layers. Visual Basic was too easy, and many serious programmers hated it just for that reason. Even though Visual basic was very easy to use, it was not very flexible to develop serious applications. SUN's Java became a very good choice for these reasons. It had the flixibility and power of C++ and at the same time easy enough to catch the attention of VB programmers.

Microsoft recognised these factors and they introducd the .NET considering all these factors. All unwanted complexities are eliminated and a pure object oriented programming model was introduced. This makes programmer's life very easy.

.NET framework comes with a single class library. And thats all programmers need to learn!! Whether they write the code in C# or VB.NET or J#, it doesn't matter, you just use the .NET class library. There is no classes specific to any language. There is nothing more you can do in a language, which you can't do in any other .NET language. You can write code in C# or VB.NET with the same number of lines of code, same performance and same efficiency, because eveyone uses same .NET class library.

What is .NET ? It is a platform neutral framework. Is a layer between the operating system and the programming language. It supports many programming languages, including VB.NET, C# etc. .NET provides a common set of class libraries, which can be accessed from any .NET based programming language. There will not be separate set of classes and libraries for each language. If you know any one .NET language, you can write code in any .NET language!! In future versions of Windows, .NET will be freely distributed as part of operating system and users will never have to install .NET separately. What is Not ? .NET is not an operating system. .NET is not a programming language.

".NET is a framework"

Are you confused by this definition? Well, that is OK. It is really confusing! We cannot define .NET as a 'single thing'. It is a new, easy, and extensive programming platform. It is not a programming language, but it supports several programming languages. By default .NET comes with few programming languages including C# (C Sharp), VB.NET, J# and managed C++. .NET is a common platform for all the supported languages. It gives a common class library, which can be called from any of the supported languages. So, developers need not learn many libraries when they switch to a different language. Only the syntax is different for each language.

When you write code in any language and compile, it will be converted to an 'Intermediate Language' (Microsoft Intermediate Language - MSIL). So, your compiled executable contains the IL and not really executable machine language. When the .NET application runs, the .NET framework in the target computer take care of the execution. (To run a .NET application, the target computer should have .NET framework installed.) The .NET framework converts the calls to .NET class libraries to the corresponding APIs of the Operating system.

Whether you write code in C# or VB.NET, you are calling methods in the same .NET class libraries. The same .NET framework executes the C# and VB.NET applications. So, there won't be any performance difference based on the language you write code.

Chapter-2 What is Visual Studio.NET ?


Many people always get confused with Visual Studio .NET (VS.NET) and .NET technology. VS.NET is just an editor, provided by Microsoft to help developers write .NET programs easily. VS.NET editor automatically generates lot of code, allows developers to drag and drop controls to a form, provide short cuts to compile and build the application etc. VS.NET is not a required thing to do .NET programming. You can simply use a notepad or any other simple editor to write your .NET code!!! And you can compile your .NET programs from the command prompt.

Well, what I said is true theoretically.. but if you decide to use notepad for .NET programming, by the

time you develop few sample applications, Microsoft would have introduced some other new technology and .NET would be outdated. You may not want that. So, let us go by VS.NET, just like every other .NET guys.

You can read more about VisualStudio.NET in the next article.

.NET supported languages

Currently .NET supports the following languages: C# VB.NET C++ J# The above languages are from Microsoft. Many third parties are writing compilers for other languages with .NET support.Difference between VB and VB.NETBelieve us, there is not much in common between VB and VB.NET other than the name. VB.NET is a totally new programming language. It just retains the syntax of old VB. So, if you are a vb programmer, probably you may like VB.NET than C# just because of the syntax.

In addition to this, VB.NET still support many of the old VB functions just for backward compatibility. But if you are a serious .NET programmer, we strongly suggest never use old VB functions in VB.NET. So, switching from VB to VB.NET is just like learning a new programming language, with very small similarities between them.

C# or VB.NET ? Which one to choose ?

As we mentioned in earlier chapters, it makes no much difference. Whether you write code in VB.NET or C#, when you compile, your code will get converted to MSIL (Microsoft Intermediate language). It is this MSIL which you deliver to your customer in the form of a DLL or EXE. The MSIL is executed by the same .NET framework, whether you wrote it originally in C# or VB.NET.

The MSIL generated by C# and VB.NET is almost 99% is the same! Many believe that C# has the power of C++ and VB.NET has the user friendliness of VB. That is not true. Both are equally powerfull and friendly.

VB.NET has backward compatibility with old Visual basic. So, it supports old vb functions. C# is a fresh, clean language. So strongly support using C# instead of VB.NET just for this clean compiler.

Many old VB guys usually like to stick with VB.NET and are kind of scared of C#. We are sure that you will not take more than few days to get familiar with C# syntax. This online tutorial is based on C# and all samples will be provided in C#.

Is it platform independent ?

Many people ask this question "Java is platform independent, what about .NET ?".

The answer is "Yes" and "No" ! The code you write is platform independent, because whatever you write is getting compiled into MSIL. There is no native code, which depends on your operating system or CPU. But when you execute the MSIL, the .NET framework in the target system will convert the MSIL into native platform code.

So, if you run your .NET exe in a WIndows machine, the .NET framework for Windows will convert it into Windows native code and execute. If you run your .NET application in Unix or Linux, the .NET framework for Unix/Linuxwill convert your code into Unix/Linux native code and execute. So, your code is purely platform independent and runs anywhere!

But wait, we said it wrong... there is no .NET framework for Unix or Linuxavailable now. Microsoft has written the .NET framework only for Windows. If you or some one else write a .NET framework for other platforms in future, your code will run there too. So, let us wait until someone write .NET framework for Linux before you run your .NET code in Linux.

Is it worth learning .NET ?

Are you sure you have a very good job now and your job is safe for next 10 years? Then, probably you don't need to waste your time to learn this new technology.

If you are not sure about the future of your job, better spend sometime and make sure you have fuel to run for next few years!

How long it will take to learn .NET ?

It all depends on how fast you can learn. If you are familiar with Visual Basic or C++, you can come to speed in .NET within 1-2 months. If you are a new to programming, we estimate 6 months will be a reasonable period to become comfortable with .NET world.

Future of .NET

Microsoft is moving all its technologies to be .NET based or .NET related. The next version of SQL Server even supports writing stored procedures in .NET languages. .NET runtime will be part of all Operating Systems by default. In short, if you like to work on Microsoft technologies for programming, .NET would be the only choice you will have.

Chapter-2

Visual Studio .NET

What is Visual Studio.NET ?

Many people always get confused with Visual Studio .NET (VS.NET) and .NET technology. VS.NET is just an editor, provided by Microsoft to help developers write .NET programs easily . VS.NET editor automatically generates lot of code, allows developers to drag and drop controls to a form, provide

short cuts to compile and build the application etc.

Understand Visual Studio .NET

VisualStudio is a very user friendly tool. But there are enough stuff to confuse any one new to Visual Studio family. The purpose of this chapter is to make you familiar with different options in Visual Studio.NET (VS.NET) We will not cover the entire visual studio guide. We are just explaining the most commonly used features of VS.NET.

When you work on any project, VS.NET has several child windows to assist you in the application development. These windows are attached on the left, bottom and right sides of the main window. You can click on these small windows to expand it and see the content of them. Some of the most commonly used child windows are explained below. Most of these windows will be enabled only when you have created a project and working on a WebForm/WinForm. Toolbox Toolbox provides all the drag and drop controls for your application. Depending on the kind of application you are working on, the toolbox will show appropriate controls and you can drag and drop them to your form. If you are developing a web application, toolbox will show ASP.NET controls and if you are developing a Windows application, it will show Windows controls (like Radio button, text box, buttons etc).

You can simply drag and drop any controls from the toolbox to your form. After you drag and drop any control, double click on the control to go the default event associated with the control. The Toolbox will be enabled only when you Picture 1 : Toolbox

have a WebForm or WinForm opened in Design mode and is usually located on TOP LEFT corner of the VS.NET.

By default, all windows including Toolbox will be displayed as Minimized (Marked as A in picture). You will see only the small icon and the text 'Toolbox' written vertically on the left bar of VS.NET. You can click on this minimized window to expand it (Expanded window is marked as C.). When you move the mouse away from the window, it will again automatically minimize. You can keep the toolbox always expanded by pressing the pushpin (Marked with B.)

The above behaviour is common for all the windows explained below. They will be minimized by default and you can point the mouse over it to expand it. Use the pushbutton to keep it expanded. Design mode & HTML mode

If you are developing a web application, you can switch between design mode and HTML mode for any web form. When you are in HTML mode, it will show you the HTML tags and ASP.NET code and you can manually edit it. YOu can switch to the Design mode by clicking the Design button on BOTTOM LEFT corner of VS.NET. In design mode, you can see how your ASP.NET page is going to

Picture 2 : Design/HTML mode

look like when some one view your web page. Also, you can drag and drop controls from the toolbox, which will automatically generate the HTML for it.

Typically, developers switch between these two modes very frequently. You can go to design mode to drag/drop controls, right click on the controls to set correct properties etc. Then, click on the HTML mode to view the generated HTML. You can manually cmake changes to the automatically generated HTML. If you change any properties in HTML mode, the changes will be reflected when you switch to Design Mode

Solution Explorer

Solution Explorer, located on TOP RIGHT corner of VS.NET, displays your solution, all projects included in each solution and the list of files in each project. They are listed in the form of a tree control.

Typically, for a single application, you will have 1 solution and one or more projects. When you create a new application, you are have to create single Visual Studio project. In more complex applications, there may be more than one projects. All these projects are grouped into a single Picture 3 : Solution Explorer

solution. Even if you do not create a solution separately, a solution will be automatically created for you.

To add a new file to your project, you can right click on the project name (JustTestis the project name in the picture) and choose the menu option Add. It will give you the option to choose a file type. You can choose an appropriate type.

For WinForms and WebForms, you can see the file in design mode and the code associated with it. Double click on any form and it will be opened in design mode. Right click on any form and select the menu option 'View Code' to view the code associate with the form. Output

Output window, located on BOTTOM LEFT corner of VS.NET, displays the result of your project compilation. When you compile your project, all errors, warnings and compilation results will be displayed in this window.

In addition to the Output window, they are few other windows located in the BOTTOM LEFT corner of VS.NET.

Task List - shows individual tasks. Typically, when you compile your project, all errors and

Picture 4 : Output

warnings will be added to your task list. You can double click on any item in the task list to go directly to the code associated with the task

.Command Window - you can execute code statements here. When you are debugging, you can evaluate the value of any variables by typing ? followed by the variable name.

Find Results - when you search for any text in file(s) using VS.NET, the results will be displayed in this window. Projects

VS.NET allows you to create several types of projects. Most of the time you will be using one of two categories: Windows Application - to create any standard windows application. ASP.NET Web Application - to create a web site.

Creating a Project

To create a new project, choose the main menu : File > New > Project.

It will give you several options. First you must select a type from the left side of the popup - you may choose Visual Basic Projects or Visual C# projects based on the language you plan to use for

development.

Picture 4 : Create new project

After selecting a type, you choose a template from the right side. You may choose Windows Application, ASP.NET Web Application or any other templatebased on the nature of the application you want. When you create a Windows Project or ASP.NET Web Project, VS.NET will add a sample file by default. You can simply Build your new project and see how it works.

Building a project

To try this, create a new Windows Project as explained above. It will create a sample form. Go to the main menu and select the menu item Build > Build Solution.

This process will compile all the files included in your project and show you the result in the Output window. If the result shows '0 failed', your build is success and your application is ready to deliver!!

To Run the application you just Built, go to the main menu and select Debug > Start Without Debugging. This will launch the application you just developed.

You can drag and drop several controls to the form and try running it.

When you compile (build) the code, if there is any errors or warnings, the details will be shown in the 'Task List' window. You can click on the specific item in this window to go directly to the line of code associated with the error.

Chapter-3

What is XML?
This article explains ...Waht is Extensible Markup Language

First what you are thinking is it a programming language like our c,c++ or its presentation language like HTML .YES its a presentation language not a programming language . Before going into deep about XML we should remember one think ie its a case sensitive language where as HTML is not a case sensitive .As i said before that its mainly used for structured documents.You don'y have any pre defined tags here .

Chapter-4 Download VS.NET


This chapter explains where to get the Visual Studio .NET Typically, Visual Studio .NET is a paid software. However, Microsoft gives several options to get free editions of VS.NET. Read the details below to find how to get a free or paid version of Visual Studio. Unfortunately, VS.NET is not a free download ! If you want to have Visual Studio 2002 or Visual Studio 2003, you need to purchase it. It is going to cost you around US$ 750 or more... However, here is a good news. You can download trial versions of Visual Studio 2005 from Microsoft. Note that, this free download is a limited version. You have to download separate versiosn of C#, VB.NET etc. Try this link to download Visual Studio - http://lab.msdn.microsoft.com/vs2005/

Chapter-5 "Hello World" Application


It is time to get started with code. In this chapter, we will show you how to write your first C# program. This chapter assumes that you have already installed and configured Visual Studio .NET in your computer.

Here we will guide you through step by step process to create your first sample .NET application. All our sample code will be using C# syntax. Readers are suggested to stick to this new elegant language, than going back to your favorite VB style.

Hello World Application using C#

Let us get started the traditional way, with a 'Hello World' application. We hope you all have Visual Studio.NET installed on your computer. If you have VS.NET, goto the menu option File > New > Project. Select 'Visual C# projects' and choose the project template 'Console Application'. This will create a default A simple C# program would look like the following.

using System; public class Hello { public static void Main (string[] args) { Console.WriteLine("Hello C# World"); } }

When you create a new Console Application, it will create a default class and you can just insert one line of code into it: Console.WriteLine("Hello C# World"); Now compile and run your first program by pressing Ctrl + F5. You will see the following result : > Hello C# World You are done! You got your first C# program successfully running.

If you do not have VS.NET, you may use any editor (including notepad) to create your first C#

program. Create a new file with the name sample.cs and type the C# code shown. Now go to the command prompt and navigate to the folder where you have the .NET framework installed. Compile your csharp file by using the command 'csc': csc c:\samples\sample.cs csc is the C# compiler. The above command will produce the output file sample.exe. You can run the sample.exe and you will see the output from the program.

Analyzing your first program See the first line of code : using System; "System" is a namespace and the "using" directive says that all classes in the "System" namespace can beused in this class without using the fully qualified name. In our class, "Console" is a class in the namespace "System". To use this class you have to actually write :System.Console.WriteLine ("..."); But the using System; directive on top of the classallows us to use the class without including the namespace. So, you can now simply write :

Console.WriteLine("...");

If you are familiar with Object Oriented Programming, you might not need more explanation for the next line - declaring a class.

public static void Main (string[] args) - is the Main method, which is thestarting point of the application.

string[] args is the list of arguments that are passed to this application. (In our case, we are not passing any command line parameters).

Console.WriteLine("...") is another line of important code. Console is a class, part of .NET class library included in System namespace. WriteLine is a method part if this class and used to printoutput to the default Console.

Chapter-6 C# Language Syntax


C# Language Syntax and essentials This article will show you the basic statements in C# language and language syntax. Declaring Variables

The following sample shows different ways you can declare a variable:

int a; int salary, incomeTax, sum; int count = 10; string name; string fullName= "Little John";
Loop Statements

while

int i = 0; while ( i < 5 ) { Console.WriteLine ( i ); ++i; }


The above loop repeates 5 times and prints the value of i. The output of above code would be like this :

0 1 2 3 4
for

for ( int i = 0; i < 5; i++ ) { Console.WriteLine ( i );

}
The above loop repeates 5 times just like the while loop and prints the value of i. The output of above code would be like this :

0 1 2 3 4
do ... while

int i = 0; do { Console.WriteLine ( i ); i++; } while ( i < 5 );


The above loop is pretty much same as the while loop. The only difference is, the condition is checked only after executing the code inside the loop. foreach

string [] names = new string[]{ "Little John", "Pete", "Jim", "Bill" }; foreach ( string name in names ) { Console.WriteLine ( name ); }
foreach loop can be used to iterate through a collection like array, ArrayList etc. The above code displays the following output:

Little john Pete Jim Bill


Conditional Operators

if ... else

This is the conditional operator, used to selectively execute portions of code, based on some conditions.

string name = "Little John"; if ( name == "Jim" ) { Console.WriteLine( "you are in 'if' block" ); } else { Console.WriteLine( "you are in 'else' block" ); }
in the above case, it prints :

you are in 'else' block


Flow Control Statements

break 'break' statement is used to break out of loops ('while', 'for', switch' etc).

string [] names = new string[] { "Little John", "Pete", "Jim", "Bill" }; foreach ( string name in names ) { Console.WriteLine ( name ); if ( name == "Pete" ) break; }
In the above sample, it iterates through the array of 4 items, but when it encounters the name "Pete", it exits the loop and will not continue in the loop anymore. The output of above sample would be :

Little John Pete


continue 'continue' statement is also used to in the loops ('while', 'for' etc). When executed, 'continue' statement will move the exection to the next iteration in the loop, without continuing the lines of code

after the 'continue' inside the loop.

string [] names = new string[]{ "Little John", "Pete", "Jim", "Bill" }; foreach ( string name in names ) { if ( name == "Pete" ) continue; Console.WriteLine ( name ); }
In the above sample, when the value of name is "Pete", it executes the 'continue' which will change the execution to the next iteration, without executing the lines below it. So, it will not print the name, if the name is "Pete". The output of above sample would be :

Little John Jim Bill


switch if you have to writeseveral if...else conditions in your code, switch statement is a better way of doing it. The following sample is self explanatory:

int i = 3; switch ( i ) { case 5: Console.WriteLine( "Value of i is : " + 5 ); break; case 6: Console.WriteLine( "Value of i is : " + 6 ); break; case 3: Console.WriteLine( "Value of i is : " + 3 ); break; case 4: Console.WriteLine( "Value of i is : " + 4 ); break; default:

Console.WriteLine( "Value of i is : " + i ); break; }


In the above sample, depending on the value of the conditional item, it executes appripriate case. In our code, since the value of i is 3, it executes the third case. The output will be as shown below:

Value of i is : 3

Vous aimerez peut-être aussi