Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

11/2/2011 Untitled Document

Source : Mindcracker Network (www.c-sharpcorner.com) P rint

C# Tutorial for Beginners: Part I

Chapter 1: About Microsoft. NET


All About Microsoft. NET

Microsoft. NET is one of the latest and new tec hnologies introduc ed by Mic rosoft Corporation.
Nowadays we use to c onnec t to the internet using a c omputer and a remote c omputer responses
via a web page and a collection of web pages are c alled as Web Sites. The Conc ept in .NET is
that these websites c an integrate with other sites and services using Standard Protoc ols like
HTTP.

Mic rosoft .NET Platform comprises of four core components suc h as:

NET Building Block Servic es suc h as file storage, c alendar c alled Passport. NET
NET Device Software whic h will run on latest Internet Devic es like Mobile Phones.
NET user experienc e suc h as integrating this technology to user c reated doc uments
(integrates with XML). For example if you c ode XML via a .NET Language like C#, it will
automatic ally c reate XML document.
NET infrastructure whic h includes NET Framework (Common Language Runtime & .NET
Framework Class Libraries) Mic rosoft Visual Studio.NET such as Visual Basic .NET ,Visual
C++.NET etc NET Enterprise Servers and Mic rosoft Windows. NET.

We c an build robust, sc alable, distributed applications with the help of .NET and the part that
helps us to develop these applic ations is called the .NET Framework. The .NET Framework
contains Common Language Runtime (CLR) and the .NET Framework class libraries also c alled as
Base Class Libraries.

All the .NET languages (like C- sharp, VisualBasic .NET, Visual C++. NET etc ) have the .NET
Framework c lass libraries built into them. The .NET c lass Libraries also supports File I/O, database
operations, XML (Extensible Markup Language) and SOAP (Simple Object Ac cess Protoc ol). For
example you can develop XML Pages by using C-sharp language.

When someone talks about .NET development, then you should understand that they are talking
about .NET Framework. It includes a Runtime environment and a set of Class Libraries which is
being used by a new language c alled C-sharp abbreviated as C# (more or less similar to
C/C++/Java family of languages) and all other .NET Languages. Simply speaking C-sharp is a new
language for developing c ustom solutions for Mic rosoft's .NET Platform.

The runtime whic h we discussed just now is also used by VisualStudio.NET. Visual Studio.NET
Provides us with a visual environment to design and develop .NET Applic ations. Every language in
VisualStudio.NET uses this runtime to exec ute its applic ations. Moreover these languages
compiles its sourc e c ode into an intermediate language upon c ompilation. Hence you c an very
well use a module written using C- sharp in a Visual Basic Applic ation. For example you c an design
a user interfac e with Visual Basic .NET and write a DLL function using C-sharp.

.NET Framework Process

1/5
11/2/2011 Untitled Document

Terms Associated with Microsoft .NET

Common Language Runtime

Common Language Runtime also c alled CLR Provides a universal exec ution engine for developers
code. It generates SOAP when it makes remote proc edure calls. CLR is independent and is
provided as part of the .NET Framework. The main features of CLR are as follows:

1. Managed Code
2. Automatic application installation
3. Memory Management
4. Automatic Garbage Collection
5. Very high level of Sec urity while exec uting

.NET Framework Class Libraries

These class libraries works with any language under the common language runtime environment.
It includes Visual Studio.NET, C-Sharp. Therefore if you are familiar with one .NET language then
you c an easily migrate to other .NET Languages. The figure given below shows the .NET
Framework hierarc hy

Note:

1. All namespac es should have to be c alled in your Program by applying the keyword using.
For example if your programs needs to c all System namespac e, then it should be applied in
the program as using System.
2. Classes c annot be called along with the using directive.
3. The using direc tive applies only to namespac es, just like importing packages in Java. Hence
the following code will result in c ompilation error, using System.Console.

However you c an create an alias like the following using mysys = System.Console. Then you have
2/5
11/2/2011 Untitled Document
to apply the alias in your program as follows mysys.writeLine("Hello C#");

Common Language Specification (CLS)

It is a set of rules that a language c ompiler must adhere to in order to create .NET Applic ations
that run in the CLR. If you are going to create a c ompiler for .NET, you have to adhere to the
rules enumerated in the c ommon language spec ific ation and this enables us to c reate a c lub of
CLS compliant languages.

Eac h suc h c ompiler will have the following features:

Complete acc ess to .NET Framework hierarchy


High level of interoperability with other c ompliant languages like Visual Basic. NET etc.

For example a Visual Basic c lass can inherit from a C# Class.

You should note that the syntaxes and other programming structures differ from eac h of these
languages. The only differenc e is that a developer well versed with a language suc h as C-sharp
can easily program in Visual Basic.NET or Visual C++.NET without investing a lot or spending too
long to learn a new language.

Chapter 2: Begin C-Sharp Programming


Getting Started with C-sharp

First of all let me welcome you to the world of this new programming language. I hope you will
have a basic idea about Objec t Oriented Programming languages because many languages like
Java, C++ have c ome by the past 5 years. However there will be no diffic ulty in learning this
language if you are a fresher, because this tutorial and the coming ones will explain all the
concepts and features right from the beginning.

Wherever required I explained the features involved in C-sharp by c omparing them with Java. This
will ensure smooth progress for experienc ed Programmers. I rec ommend you to install the
appropriate software's outlined in the next sec tion before learning this new language.

Basic Requirements needed to begin C-sharp Programming

1. .NET Framework Software Development Kit and


2. An Editor (like Notepad or DOS Editor) to write source codes.

Optional Requirements:

1. Visual C#. NET or


2. Visual C++ 6.0 inc luded with VisualStudio 6.0

Installing .NET Framework SDK

As a first step you should install .NET SDK on your computer to begin C-sharp Programming. It
can be downloaded from the Microsoft's Website. It is also available with almost all the popular
computing magazine CD'S. It also comes with the c omplete doc umentation in the form of HTML
Help.

At the time of this writing only beta version of the kit is available. This kit enables you to compile
& exec ute the source code in C#, Visual Basic by using its built-in Command line Compiler (csc
and vbc) and runtime Just In Time (JIT) Compiler. This is similar to Java Compiler (javac) and
Java Interpreter (java) inc luded
with Java Development Kit.

You can also develop applic ations with C-sharp and Visual Basic by using Visual Studio.NET
languages like Visual C#.NET and Visual Basic .NET. This will help you to develop windows based
applications easily and with limited effort because you don't have to devote too muc h time in
designing the user interface (Usage of WinForms). The only work left for you to do is to write the
codings appropriately as per the .NET Standards.

3/5
11/2/2011 Untitled Document
About the Editors

Notepad is the best c hoice among developers using .NET SDK to develop C# applic ations.
However it is not the most suitable editor sinc e it does not support syntax c oloring, c ode
numberings etc.

Developers c an use Visual C++ 6.0 inc luded with Visual Studio 6.0. However they should make
some tweaking in the registry before using the same. It supports syntax colorings and other
features such as finding line numbers (Ctrl+G). However it is dangerous for a new user to make
changes in registry. Henc e only advanc ed and experienced users c an be able to use Visual Studio
6.0 for developing C#. It is not possible to c ompile and exec ute the applications from the Visual
C++ 6.0 Environment. Henc e there is not muc h use exc ept some of the features listed above.

VisualStudio.NET provides all the integrated tools and wizards for creating C# and Visual Basic
applications. It also supports features suc h as intellisense, Dynamic help. Moreover you can
compile and exec ute your applications from the IDE itself. Henc e in order to experienc e the
power of developing the .NET applic ations, you should try VisualStudio.NET.

Many Third Party editors are now available either from magazine's CD'S or can be downloaded
from the internet. However it is up to you to dec ide upon whic h editor to use. I recommend you
to try one common editor and learn the language in full.

Chapter 3: Your First Hello C# Program


Getting Started with Hello C-Sharp

After understanding the fundamentals of .NET and its Structure, let us now move on to look at a
classic Hello C# Program. As I mentioned in the previous tutorial, you c an use any text editor as
per your Convenienc e.

Listing 3.1 gives the coding for this simple Hello C# Program.

using System ;
Class Hello
{
Public static void Main ()
{
Console.writeLine ("Hello C#");
}
} //end of the main

What to do next: -

Save the following file as Hello.cs. c s is an extension to indicate C-sharp like .java for a Java
Source File. You have to supply this extension while saving your file, otherwise the code will not
compile correc tly. The saved file will be of .c s.txt extension.

Compile the above Code by giving the following Command at the Command Prompt csc Hello.cs

If there are compile errors then you will be prompted ac c ordingly, otherwise you will be getting a
command prompt with the copyright information. Keep in mind that the compiler generates MSIL.

Your next job is to execute the program to view the final Output. For that purpose you have to
simply give Hello at the c ommand Prompt. If everything goes on well, a message Hello C# will
be printed on the Sc reen.

You can view the MSIL Code generated by any .NET Language with the help of an Utility called
Intermediate Language Disassembler or shortly ILDASM. This utility will display the applic ation's
information in a tree like fashion. Sinc e the c ontents of this file is read only, a Programmer or
anybody acc essing these files c annot make any modifications to the output generated by the
Source Code.

To view the MSIL Code for the above Hello C# Program, open Hello.exe file from the ILDASM tool.
4/5
11/2/2011 Untitled Document
In Windows 98, ME & NT this tool c an be ac c essed via start - programs - Mic rosoft .NET
Framework SDK - Tools - IL Disassembler

Detailed Review of the above Program: -

I am giving below the proc ess occ urring to the source code, during the c ompilation and exec ution
stages onc e again for your referenc e.

Compilation Stage: -

C# Compiler produces MSIL as output after compilation. It is itself a c omplete language and it is
more or less similar to Assembly language. MSIL stands for Microsoft Intermediate Language. The
same thing happens to any applic ation written in any CLR Compliant Language like Visual
Basic .NET, Visual C++.NET etc .

Execution Stage: -

The MSIL is then compiled into native CPU instruc tions by using JIT (Just in Time) c ompiler at the
time of the program exec ution. The Sourc e Code is rec ompiled and exec uted only after making
any c hanges to it .

Now let us analyze the Hello C# Program as a whole. I am giving below the code onc e again for
your reference. Please note that the line numbers are given for clarific ation and explanation and
is not a part of the Sourc e Code.

using System ;
class Hello
{
public static void Main ()
{
Console.writeLine ("Hello C#");
}
}
//end of the main

Line Number wise Analysis


Line - 1: - It is called as the namespace. Simply speaking, a namespac e is a c ollec tion of .NET
Classes similar to pac kages in Java.

Line - 2: - It is the class dec laration similar to Java & C++

Line - 3: - This is the main method whic h is required in all C# applic ations.

Line - 4: - This c ode will print Hello C# onto the Console. Here Console is the c lass belonging to
System namespac e and writeLine is the static method belonging to Console Class.

Line - 5:- The Opening and Closing c urly rac es are similar to that of C++/Java.
Thank you for using Mindcracker Network

5/5

You might also like