C, C++
C#
(pronounced as C Sharp) is a new object oriented programming language,
very similar to Java, with the power of C++ and smoothness of Visual
Basic. This language was developed by Microsoft as a part of their .NET
initiative, for building a wide range of enterprise applications that
run on the .NET framework. C# cleans up many of the syntactic
peculiarities of C++ without diluting much of its flavor. You can say
that C# is the combination of 70% Java, 10% C++, 5% Visual Basic and 15%
new.
In
today's web economy, businesses are always forced to respond to
competitive pressure faster than ever before. Developers are asked to
reduce cycle time and enhance the program with incremental revisions
rather than a single huge version. C# has been designed keeping these in
mind. The language is designed to help developers do more with fewer
lines of code and lesser error opportunities.
Background
In
June 2000, Microsoft announced this new programming language called C#
along with .NET. This strongly typed object oriented language was
designed to give the optimum blend of simplicity, expressiveness and
performance.
The
C# language was built with the perception of many languages, most
notably Java and C++. It was co-authored by Anders Hejlsberg and Scott
Wiltamuth. Hejlsberg's previous experience in programming language and
framework design can be readily seen in the syntax of the C#
language.
The name
According
to the ECMA-334 C# language specification, the name of this language is
written in Latin Capital Letter C followed by the Number Sign # and
pronounced as C Sharp. It is said that Microsoft perhaps named
this language C# may be to mean evolution from the C++ language, with
the # symbol resembling two ++ symbols merged together or four + symbols
arranged together.
C# and .NET platform
The
.NET platform is centered on a Common Language Runtime or CLR and a set
of libraries, which can be exploited by a wide variety of languages,
which are able together by all compiling to an intermediate language
(IL). C# and .NET are quite symbiotic: some features of C# are there to
work well with .NET while some features of .NET work well with C#
(though .NET aims to work well with many languages).
C#
and .NET are closely coupled. In fact, every application you create
will begin with the connection to the .NET framework. Though developers
can write unmanaged code under certain circumstances, but most people
use c# for its intended purpose of writing distributed applications. It
is the Common Language Runtime or CLR that allows such interoperability
in .NET. Within this runtime C# shares the following set of resources:
- Object oriented programming features like inheritance, polymorphism, exception handling, garbage collection etc.
- Security model
- Type system
- All .NET base classes and many .NET framework classes
- Development, debugging and profiling tools
- Execution and code management
C# and Java
C#
has inherited a lot of Java benefits such as Garbage Collection,
restricting pointer usage etc. However, it has also kept a lot of the
C++ elegancy that Java threw away as well as some powerful VB features.
Below is a list of some of the features that C# share with Java:
- Compiles code into machine independent language independent code to run the application in a managed execution environment.
- Garbage collection coupled with the elimination of pointers
- Powerful reflection capabilities
- No header files, you can declare one class before another with circular dependencies
- Classes descend from object and must be allocated on the heap with new keyword
- Thread support by putting a lock on objects at the time of entering code marked as locked/synchronized
- Interfaces, with multiple inheritance or single inheritance implementations
C# Features
C#
is the programming language that depends strongly on Common Language
Infrastructure (CLI), as it was designed to take advantage of the
features that CLI provides. Applications written in C# require an
implementation of the Common Language Runtime (CLR) to execute.
Type Safety
C#
is type safe language. This means, for instance, you cannot use any
uninitialized variables in C#. The compiler will notify you if you try
to use a variable before you have initialized it to some valid value.
With
C# you can no longer just walk past the end of an array, as you have
been able to do in C and C++. Syntax for array declaration is different.
For example, instead of int a[5]; C# declares array as int() a
= new int[5];
Object Oriented
In C#, even a simple data type can be treated as objects. This means an int can have methods associated with it. Look at the code below, where ToString method has been used to get a string value for an int.
int Counter=10;
Console.Write (Counter.ToString());
Simplified Syntax
C#
attempts to simplify the syntax to be more consistent and more logical
while also removing some of the more complex features of C++. E.g.
pointers are not required in C#. As a type safe language, C# does not
allow direct memory manipulation, so pointers are not needed.
Header
files have also been removed from C#. The namespace operator :: and the
reference operator -> have been replaced with a single operator, the
period (.).
C#
also removes memory management issues by using .NET's garbage
collection scheme. Items that are no longer referenced are marked for
garbage collection, and the framework can regain this memory as
needed.
XML Comments
C#
supports adding XML comments to code. These comments are placed in XML
format and can then be used as needed to document your code. This
documentation can include example code, parameters, reference to other
topics etc.
Components
Namespaces
in C# replace a lot of the headaches from the COM world. Using C#, you
simply import a namespace and then begin using the classes in that
component -- no registry lookups or COM plumbing required.
Cross language capabilities
C#
has the ability to allow you to interoperate with any other language on
the .NET platform. It also supports the concept of error handling
across different languages; you can access any .NET exceptions, which
are consistent across any .NET language.
Getting Started
To start working with C# you need the following:
- A .NET compatible operating system like Windows 2000/2003/XP
- The
.NET framework (runtime platform that sits on the operating system) and
SDK (development kit in which you actually develop .NET applications)
- C# compiler and an editor.
There
are several options for obtaining a compiler for writing C# programs. A
free option is to download .NET Framework SDK and use Notepad. Of
course there are many editors and IDE options available like Microsoft
Visual Studio .NET and Borland C#Builder for you to choose for your
development environment. After installing the framework and SDK, it is
important to know the path of the directories where they have been
installed.
This is how a program is developed using C#:
- The actual code is written in an editor such as Notepad or an IDE such as VS .Net.
- Once the file or project is saved, the CLR engine of the .NET framework compiles it.
- In
the first step of compilation, C# source code gets converted into
Intermediate Language (IL). This is then converted into CPU specific
native language.
We
will start off with a real simple C# application. This will explain
some of the basic concepts like how to start a program, how to structure
the code and gain some familiarity with the syntax of a C# program.
In C# language, everything is built as a class. Therefore a program starts with the class keyword followed by a name.
The actual building of a class starts with an opening curly bracket { and a closing curly bracket }™
The
assignments of a program written in C# are carried in entities called
methods. A method is simply a section of code that takes care of a
particular detail for the functionality of a program. To create a
method, you must specify a name, which is followed by an opening and
closing parenthesis.
Here the method is Sell.
A method's job is to carry a specific assignment within a program and
it could provide a value once the assignment has been carried. If the
method doesn't give back a value, then it is considered void. The type
of value that a method can provide or return is written on the left
side of the method name.
class MyShop
{
void Sell()
{
}
}
The assignment that a method carries is included between an opening { and a closing }.
The
most regularly used method of a C# program is called Main. Therefore,
the minimum you can write a program with, is as follows:
using System
public class MyShop
{
public static void Main()
{
Console.WriteLine("Welcome to My Shop!");
}
}
The using
keyword is very useful when you use a number of classes defined in the
system; instead of writing System.Console at line 6 you can write only
Console.
Now save the file as Welcome.cs.
Note:
A file for a C# program has the extension .cs
The effect is to write the text Welcome to My Shop! to
the output console. Now at DOS prompt, go to the directory where you
saved the file Welcome.cs and type the following in command line:
csc.exe Welcome.cs
This
compiles your source code and produces a file named Welcome.exe, which
can then be executed. As you run this file you will see
Welcome to My Shop!
Other programs can be compiled similarly by substituting their file name instead of Welcome.cs.