Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » C# Language » An Introduction to C#

An Introduction to C#

This tutorial explains the basics of C#, a modern object oriented programming language that was designed by Anders Hejlsberg at Microsoft.

Page Views : 9331
Downloads : 55
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
HelloWorld.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

This tutorial explains the basics of C#, a modern object oriented programming language that was designed by Anders Hejlsberg at Microsoft.

A QUICK INTRODUCTION

The pursuit to teach any new language usually begins with the classic Hello World program. This tutorial does not break that tradition

/*
* HelloWorld.cs
* Version 1.1
* A program that prints Hello, world! on screen
*/
class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("Hello, world!");
}
}

Compiling and running:

There are currently two approaches to compiling and running this program. The more rugged solution would be to download the Microsoft.NET platform SDK, from microsoft.com and compile it using the C# command line compiler. This produces an Intermediate Language .exe that can be executed from the console.The second and more costly option would be to purchase Microsoft Visual Studio. NET. It has a neat IDE, in which the above program could be compiled under a new Solution.

Using the first option
csc HelloWorld.cs

Microsoft (R) Visual C# Compiler Version [CLR version]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

In this admittedly simple case, you do not have to specify anything other than the file to compile. In particular, C# does not use the additional step of linking that is required by C/C++ etc.
The default output of the C# compiler is an executable file of the same name, and running this program generates the following output:

C:\\HelloWorld\cs>HelloWorld
Hello, world!

Programmers familiar with JAVA will be amazed at how close C# syntax is to JAVA As in c, c++, java // /*, */ are valid comment lines in C# also. Any line started by // or contained within /* and */ is ignored by the compiler. However in C#, XML statements may be embedded within comments and may later be used to generate documentation for the program.

Just as the entry point to a c/c++/java program is main, the entry point to a C# program is Main() *with a capital M*. But unlike in the case of the former, no arguments are passed to Main(), and it returns a void (a keyword that indicates the function does not return anything).

public and static are qualifiers for the entry point function Main(). More on this later.

From the Official Documentation:

"The compiler requires the entry point to be called Main. The entry point must also be marked with both public and static. In addition, the entry point takes no arguments and does not return anything (although different signatures for more sophisticated programs are certainly possible)."

Since C# is strictly an Object Oriented language, there needs to be at least one class in every program. In HelloWorld.cs the class that contains Main() is given the name HelloWorld (Note that the class name and the file name need not match. This program can be put into a file called myFirstProgram.cs. It will compile and run properly even then).

From the Official Documentation:

"In C#, all code must be contained in methods of a class. So, to house the entry-point code, you must first create a class. (The name of the class does not matter here)."

For those familiar to C/C++/JAVA programming System.Console.WriteLine() appears to be a function call. It is! It takes one argument and displays it on the console.

NAMESPACES

Namespaces were used earlier in C++ and now in C#. Namespaces, as the name suggests are spaces that are used to contain a set of programming entities such as classes, methods etc.

From the Official Documentation:

"C# programs are organized using namespaces. Namespaces are used both as an "internal" organization system for a program, and as an "external" organization system-a way of presenting program elements that are exposed to other programs"

This has several advantages...
In a large project, two different persons working on it may name a class similarly, which may lead to ambiguity. Namespaces offer a neat solution. Any number of independent programmers may give the same name to classes they create, provided they put everything into their own unique namespace.

For example there could be a two namespaces called Console and GUI. Both could contain a function named PutText() that displays text in the command line in case of the former and in a window in the case of the latter. Such functions could be accessed by saying Console.PutText() for the console version and GUI.PutText() for the other.

The above example was a generic programming example. In C# since every method
needs to reside within a class, different namespaces could contain different classes with same names.

Getting back to HelloWorld.cs, I said at the end of the previous section that System.Console.WriteLine() is a function call. Well, it is only partly true. Actually System is a namespace that contains a class called Console, and Console contains a static method called Write Line().

A Quick note on static methods:

When a class contains a method, it cannot be accessed, like a normal c style function. It needs to be invoked using an object-which is defined as an instance of that class. But it is often required that some methods of a class be accessed without an object. To define such functions we include a qualifier called static. That explains why static is added to the Main() function. The CLR needs to call Main(), the entry point without creating any object for it.

The .NET Framework defines more than 90 namespaces that begin with the word
System. System contains a class called Console that mostly contains methods that are related to console operations.

Since HelloWorld.cs only contained one statement that called WriteLine() method in
Console class, we fully qualified it using Namespace name . Class name . Method name Image we were to make a thousand calls to it from our program... It would be tedious to reference the method using the Namespace name every time. For this purpose C# includes a keyword called using. Lets now re-write the same example...

/*
* HelloWorld.cs
* Version 1.2
* A program that prints Hello, world! on screen
*/

using System;
class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}

The above program explains clearly, the purpose of 'using'. You specify a namespace only once in the using statement and then avoid typing the namespace name to refer to classes that come under it. In the above program the amount of typing avoided may seem to be trivial. Just imagine you were using the namespace System.Windows.Forms,or your own namespace called ProgramminginCSharp.MyPrograms.Introduction?

From the Official Documentation:

"'Using' directives facilitate the use of namespaces and types defined in other namespaces. Using directives impact the name resolution process of namespace-or-type-names (Section 3.8) and simple-names (Section 7.5.2), but unlike declarations, using directives do not contribute new members to the underlying declaration spaces of the compilation units or namespaces within which they are used."

All along I've been talking about, putting classes into namespaces. But in the two versions of out program HelloWorld.cs, our class has not been put inside any namespace. What happens then? They go into the global namespace. Lets now try to write version 3 of this program that uses namespaces profitably.

/*
* HelloWorld.cs
* Version 1.3
* A program that prints Hello, world! on screen
*/
using System;
namespace CSharpTutorial
{
class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
}

Just enclose the class within a namespace called CSharpTutorial. Just as you access,pre defined classes like System.Console, you can access this class from other programs by referring to it as CSharpTutorial.HelloWorld (this class is featureless however, to be accessed by other classes).

Namespaces also play an important role in the organization of the .NET framework documentation. All the classes have been organized by the Namespaces under which they have been grouped.

A sample that accompanies the .NET Framework SDK itself is a command-line type locater that supports sub string searches. You can use it to explore classes contained in the various namespaces. (JAVA developers will find it similar to javap). To build and use this sample, follow the instructions contained in the Readme.htm file that is located in the InstallDirectory\Samples\Applications\TypeFinder subdirectory.

Here is an example of how to use the type finder:

C:\\TypeFinder\CS>findtype String

class System.IO.StringWriter

That concludes the tutorial. Hope you got a feel of C#!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Kaushik Srenevasan
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.