Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » C# Tutorial for Beginners: Part I

C# Tutorial for Beginners: Part I

Three chapter of this tutorial explains basics of .NET, C# and how to write your first C# program.

Technologies: .NET 1.0/1.1, .NET 1.0/1.1,Visual C# .NET
Total downloads :
Total page views :  175044
Rating :
 2.88/5
This article has been rated :  16 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor



Chapter 1:   About Microsoft. NET

All About Microsoft. NET

Microsoft. NET is one of the latest and new technologies introduced by Microsoft Corporation. Nowadays we use to connect to the internet using a computer and a remote computer responses via a web page and a collection of web pages are called as Web Sites. The Concept in .NET is that these websites can integrate with other sites and services using Standard Protocols like HTTP.

Microsoft .NET Platform comprises of  four core components such as:

  • NET Building Block Services such as file storage, calendar called Passport. NET
  • NET Device Software which will run on latest Internet Devices like Mobile Phones.
  • NET user experience such as integrating this technology to user created documents (integrates with XML). For example if you code XML via a  .NET Language like C#, it will  automatically create XML document.
  • NET infrastructure which includes  NET Framework (Common Language Runtime & .NET Framework Class Libraries) Microsoft Visual Studio.NET such as Visual Basic.NET ,Visual C++.NET etc  NET Enterprise Servers and Microsoft Windows. NET.

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

All the .NET languages (like C-sharp, VisualBasic.NET, Visual C++. NET etc) have the .NET Framework class libraries built into them. The .NET class Libraries also supports File I/O, database operations, XML (Extensible Markup Language) and SOAP (Simple Object Access Protocol). 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 called 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 custom solutions for Microsoft's .NET Platform.

The runtime which we discussed just now  is also used by VisualStudio.NET. Visual Studio.NET Provides us with a visual environment to design and develop .NET Applications. Every language in VisualStudio.NET uses this runtime to execute its applications. Moreover these languages compiles its source code into an intermediate language upon compilation. Hence you can very well use a module written using C-sharp in a Visual Basic Application. For example you can design a user interface with Visual Basic.NET and write a DLL function using C-sharp.

.NET Framework Process

Terms Associated with Microsoft .NET

Common Language Runtime

Common Language Runtime also called CLR Provides a universal execution engine for developers code. It generates SOAP when it makes remote procedure 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 Security while executing

.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 can easily migrate to other .NET Languages. The figure given below shows the .NET Framework hierarchy 


                
Note:

  1. All namespaces should have to be called in your Program by applying the keyword using. For example if your programs needs to call System namespace, then it should be applied in the program as using System.
  2. Classes cannot be called along with the using directive.
  3. The using directive applies only to namespaces, just like importing packages in Java. Hence the following code will result in compilation error, using System.Console. 

However you can create an alias like the following using mysys = System.Console. Then you have 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 compiler must adhere to in order to create .NET Applications that run in the CLR. If you are going to create a compiler for .NET, you have to adhere to the rules enumerated in the common language specification and this enables us to create a club of CLS compliant languages.

Each such compiler will have the following features:

  • Complete access to .NET Framework hierarchy
  • High level of interoperability with other compliant languages like Visual Basic. NET etc.

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

You should note that the syntaxes and other programming structures differ from each of these languages. The only difference is that a developer well versed with a language such 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 Object Oriented Programming languages because many languages like Java, C++ have come by the past 5 years. However there will be no difficulty 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 comparing them with Java. This will ensure smooth progress for experienced Programmers. I recommend you to install the appropriate software's outlined in the next section 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 included 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 complete documentation 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 & execute 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) included
with Java Development Kit.

You can also develop applications 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 much 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.

About the Editors

Notepad is the best choice among developers using .NET SDK to develop C# applications. However it is not the most suitable editor since it does not support syntax coloring, code numberings etc. 

Developers can use Visual C++ 6.0 included 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. Hence only advanced and experienced users can be able to use Visual Studio 6.0 for developing C#. It is not possible to compile and execute the applications from the Visual C++ 6.0 Environment. Hence there is not much use except 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 such as intellisense, Dynamic help. Moreover you can compile and execute your applications from the IDE itself.  Hence in order to experience the power of developing the .NET applications, 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 decide upon which 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 can use any text editor as per your Convenience.

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. cs 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 correctly. The saved file will be of .cs.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 accordingly, 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 command Prompt. If everything goes on well, a message Hello C# will be printed on the Screen.

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 application's information in a tree like fashion. Since the contents of this file is read only, a Programmer or anybody accessing these files cannot 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. In Windows 98, ME & NT this tool can be accessed via  start - programs - Microsoft .NET Framework SDK - Tools - IL Disassembler

Detailed Review of the above Program: -

I am giving below the process occurring to the source code, during the compilation and execution stages once again for your reference.

Compilation Stage: -

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

Execution Stage: -

The MSIL is then compiled into native CPU instructions by using JIT (Just in Time) compiler at the time of the program execution. The Source Code is recompiled and executed only after making any changes to it .

Now let us analyze the Hello C# Program as a whole. I am giving below the code once again for your reference. Please note that the line numbers are given for clarification and explanation and is not a part of the Source 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 namespace is a collection of .NET Classes similar to packages in Java.

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

Line - 3: - This is the main method which is required in all C# applications.

Line - 4: - This code will print Hello C# onto the Console. Here Console is the class belonging to System namespace and writeLine is the static method belonging to Console Class.

Line - 5:- The Opening and Closing curly races are similar to that of C++/Java. 


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Anand Narayanswamy
Anand Narayanaswamy is a graduate of the University of Kerala. He is currently working as an instructor in a institution affiliated to Manipal Academy of Higher Education at Thiruvananthapuram, Kerala State, India. He is having over 2+ years of experience in the following skills: Java, JavaScript, Visual Basic, ASP, XML, Microsoft FrontPage, HTML, Microsoft Visual Interdev, NT Server Administration, SQL Server.  Currently he is exploring the possibilities and the features of C# and related .Net Technologies.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
errors in tutorialStephane12/8/2007
There are 2 errors that make the program give errors 1st - It's class NOT Class 2nd - It's WriteLine not writeLine Took me a while on that one :)
Reply | Email | Delete | Modify | 
error in tutorialManidipa1/23/2008
In the main() function we should pass String[] args as parameter
Reply | Email | Delete | Modify | 
 
 
error in tutorialsoma1/31/2009
it is not an mandatory thats why in this not specified
Reply | Email | Delete | Modify | 
.NET SDK mike2/15/2008
how can i install .NET SDK on my computer?
Reply | Email | Delete | Modify | 
Lengthy and texty for a beginner's tutorialTom10/28/2008
I'm an experienced programmer looking for something to show a friend of mine that's a beginner, and this was way too much info for a beginner to take in. Needs to start out way more basic, more graphics and code, and less text. Also, as with the comments above, make sure everyone understands C# is VERY case-sensitive, both for the reader AND the writer of this site. -Tom
Reply | Email | Delete | Modify | 
.NET downloadTom10/28/2008
Install .NET from Microsoft. Go to Google.com and search for ".NET Framework", "download", and site:microsoft.com. Get 1.1, the security update for 1.1, 2.0, and 3.5.
Reply | Email | Delete | Modify | 
CSC?Craig2/9/2009
What did I do wrong? I dl and installed the .net sdk (3.5) I created the file in notepad and saved it as hello.cs. I went to the cmd prompt, went to the directory that the file is in. typed csc hello.cs and received the following: 'csc' is not recognized as an internal or external command, operable program or batch file. Where did I go wrong?
Reply | Email | Delete | Modify | 
 
 
Re: CSC?Skunny2/15/2009
You have to use the command prompt provided with the SDK.
Reply | Email | Delete | Modify | 
 
Re: CSC?Raul3/5/2009
you can´t write code in notepath you first should install Visual C# 2008 Express or 2005 and then create a new project in console application and write Console.WriteLine("Hello world");
Reply | Email | Delete | Modify | 
 
Re: CSC?pr3/23/2009
What did I do wrong? I dl and installed the .net sdk (3.5) I created the file in notepad and saved it as hello.cs. I went to the cmd prompt, went to the directory that the file is in. typed csc hello.cs and received the following: 'csc' is not recognized as an internal or external command, operable program or batch file. Where did I go wrong?
Reply | Email | Delete | Modify | 
good starting point for mesmv4/14/2009
though it has lot of information, i feel it as a good starting point for beginners like me who seeks more information before doing anything. It has some typos, but that doesn't matter as long as you get what is presented there. Kudos Anand
Reply | Email | Delete | Modify | 
creating gamesJohanna4/26/2009
hi, i just want to know.. how much capacity of memory needed when making a games program.. :)
Reply | Email | Delete | Modify | 
Thanks.kujotypyduwomoq6/27/2009
I was surprised to see so many negative comments below.  I've only skimmed this article so far, and plan to come back to it for a thorough reading soon, but it looks concise and informative -- just what I was looking for.  Obviously it's not intended for beginners at programming itself, but for programmers who are just starting with C#.
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved