Introduction to C#

Understanding the C# Language

Microsoft developed C#, a new programming language based on the C and C++ languages. Microsoft describes C# in this way: ”C# is a simple, modern, object–oriented, and typesafe programming language derived from C and C++. C# (pronounced c sharp) is firmly planted in the C and C++ family tree of languages and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of visual basic and raw power of C++.”

Anders Hejlsberg, the principal architect of C#, is known for his work with Borland on Turbo Pascal and Delphi (based on object –oriented Pascal). After leaving Borland, Hejlsberg worked at Microsoft on Visual J++.

Some aspects of C# will be familiar to those who have programmed in C, C++, or Java. C# incorporates the Smalltalk concept, which means everything is an object. In other words, all types in C# are objects. C# properties are similar to visual basic properties. The Rapid Application Development (RAD) goal in C# is assisted by C#’s use of concepts and keyword, such as class, struct, statement, operator, and enum. The language also utilizes the concepts contained in the Component Object Model (COM) architecture.
 
Unlike Visual Basic or Delphi, events are type in C# and can belong to an object. Members of a class object can have variables, methods, properties, and event. Attributes are another nice feature of the language.


NOTE: C# is a case – sensitive language.
 
C# and Open Source: The Mono Project

C# and Common Language Infrastructure (CLI) is under consideration at the European Computer Manufacturers Association (ECMA). You’ll probably see C# running on Unix and Linux operating systems. It’s no wonder you see many C# compilers emerging from different development tool companies.

Ximian, the Boston-based company that’s well–known for its open-source product, GNOME is working on a Linux-based, .NET open–source project called the Mono Project. The Mono Project incorporates key .NET–compliant component, including a C# compiler, a Common Language Runtime (CLR), a Just–In-Time (JIT) compiler, and a set of class libraries that will let developers create .NET application that run on Windows, Linux, or Unix platforms. You’ll find more information on the Mono project at http://www.ximian.com/mono/ or at the ECMA Web site at http://www.ecma.ch./.

How and Why C# Came to Be

It was a dream of developers to have a language as powerful and as fast as C++, yet as easy to program as Microsoft’s visual basic and Borland’s Delphi. Microsoft planned its. .NET framework and C# language for years.

Prior to .NET the most popular programming languages for Web and Windows development were Visual Basic, Java, Visual C++, and Delphi. Each of these languages has its pros and cons. Visual Basic and Delphi are easy to use and simple to program; however, they’re not truly object-oriented and they lack the flexibility to develop complex programs. Java is good for Web development, but it still lacks in its object-oriented aspects and in its capability to develop complex windows application. Java’s platform independence however is a plus. Visual C++ is a great tool to develop windows and COM-based applications, but it’s not as easy to use as visual Basic or Delphi.

Microsoft wanted to develop an integrated tool with all the positive features of these languages and more. C# is the result. C# is as easy to use and as productive as Visual Basic and Delphi. Because it’s derived from C and C++, it doesn’t leave the C++ programmer at the beginning of the learning curve. C# also has the power and flexibility of C++. Because of its platform-independent nature (companies are still working on supporting C# on other operating systems such as Unix and Linux), you can deploy C# applications onto any. NET-supported platform .C# also provides web development similar to java with the help of ASP.NET.

Characteristics and Features of C#

As explained, C# was developed as a language that would combine the best features of previously existing Web and Windows programming languages. I’ll discuss some of the primary characteristics of C# in the following section.

Modern and Object Oriented

A modern language is one that provides latest features and tools for developing scalable, reliable, and robust industry–standard applications. C# is a modern language. The current trend in programming is Web development, and C# is the best language for developing web application and components for the Microsoft .NET platform.

As mentioned, C# is an object–oriented language. It supports all the basic object oriented language features: encapsulation, polymorphism, and inheritance. Unlike C++, C# doesn’t support multiple inheritance; however, there are always workarounds if you really need this capability. A few articles are available on C# corner for more information on such workarounds.

Simple and Flexible

C# is as simple to use as Visual Basic, in that everything in C# represented as an object. All data type and components in C# are objects. C++ programmers are sometimes confused when choosing different access operators to process object. With C# you use a dot (.) operator to access the object members.

Programmers use C# to develop both managed and unmanaged code. Managed code is code managed through the CLR module. It handles garbage collection, typesafety, and platform-independence behavior. Unmanaged code, on the other hand is code run outside the CLR, such as an ActiveX control.

C# provides the flexibility of using native Win 32 application programming interface (API) and unmanaged code through COM+. C# enables you to declare unsafe classes and members having pointers, COM interfaces, structures, and native APIs. Although the class and its member are not typesafe, they still can be executed from managed code using COM+. Using the N/ Direct features of C# and COM+, you can use the C language API. With the help of the COM+ run-time and the COM+ Common Language Specification (CLS), you can access the COM and COM+ API. Using the Sysimport attribute, you can even access native Windows API (DLLs) in C#.

Typesafety

C# is a typesafe language. All variables and classes (including primitive type, such as integer, Boolean, and float) in C# are a type, and all type are derived from the object the object type.

The object type provides basic functionality, such as string conversion, and information about a type. C# doesn’t support unsafe type assignments. In other words, assigning a float variable directly to a Boolean variable is not permitted. If you assign a float type to a Boolean type, the compiler generates an error.

C# has two kinds of type: value type and reference types. All value types are initialized with a value of zero, and all reference types are automatically initialized with a null value (local variable need to be initialized explicitly or the compiler throw a warning). The “Type in C#” section of this article will discuss types in more detail.
 
Automatic Memory Management and Garbage Collection


Automatic memory management and garbage collection are two important features of C#. With C#, you don’t need to allocate memory or release it. The garbage collection feature ensures that unused references are deleted and cleaned up in memory. You use the new operator to create type object, but you never need to call a delete operator to destroy the object. If the garbage collector finds any unreferenced object hanging around in memory, it removes it for you. Although you can’t call delete directly on an object, you have way to get garbage collector to destroy objects.

Versioning Control and Scalable

If you’re a Microsoft Window developer, you should be familiar with the expression DLL hell, which refers to having multiple versions of the same Dynamic Link Library (DLL) and not having backward and forward compatibility. For example, you can’t run programs written in Microsoft Foundation class (MFC) version4.0 on systems with MFC version 3.0 or earlier. This is one of the biggest challengers for a developer, especially if you’re developing MFC applications.

The C# model is based on namespaces. All interfaces and classes must be bundled under a namespace. A namespace has classes as its members. You can access all the members or just a single member of a namespace. Two separate namespaces can have the same class as their member.

C# also supports binary compatibility with a base class. Adding a new method to a base class won’t cause any problems in your existing application.

.NET assemblies contain metadata called manifest. A manifest stores information about an assembly such as its version, locale, and signature. There is no concept of registry entries for handing compatibility. In .NET, you simple put your assembly into one global folder if you want to make it sharable; otherwise, you put it in a private folder for private use only.


Language and Cross- Platform Interoperability

C#, as with all Microsoft.NET-supported language, shares a common .NET run-time library. The language compiler generates intermediate code, which a .NET supported compiler can read with the help of the CLR. Therefore, you can use a C# assembly in VB.NET without any problem, and vice versa.

With the full support of COM+ and .NET framework services, C# has the ability to run on cross-platform systems. The Web-based applications created from .NET use an Extensible Markup Language (XML) model, which can run on multiple platforms.

C# Editors

Before starting your first C# application, you should take a look at the C# editors available for creating applications. Visual Studio (VS) .NET Integrated Development Environment (IDE) is currently the best tool for developing C# applications. Installing VS .NET also installs the C# command-line compiler that comes with the .NET Software Development Kit (SDK).

If you don’t have VS.NET, you can install the C# command-line compiler by installing the .NET SDK. After installing the .NET SDK, you can use any C# editor.

If you can’t get one of these editors, you can use a text editor, such as Notepad or Word pad.
Ebook Download
View all
Learn
View all