Contents
Chapter 1. Introduction to C#
1.1 Introduction
C# is a simple, modern, object-oriented, versionable, compatible, flexible, interoperable and type safe programming language originated from C and C++. It combines the prominent features of modern programming languages like C++, Java, Visual Basic, etc. It is used mainly to create .NET applications. The .NET is the most modern development platform of Microsoft that enables the development of component-based applications. The .NET platform supports more than 20 different languages and the list is growing day by day.
The C# compiler is considered to be the most efficient compiler in the .NET family and a major part of the .NET base classes libraries itself are written in C#.
1.2 Evolution of C#
In the past two decades C and C++ were the most powerful and widely used languages for developing commercial and business softwares. These languages were often criticized for its longer development time and hence reduced productivity. So the developer community was longing for a highly productive and powerful language for their development needs. They required an environment that supports the emerging web standards and one that provides easy integration with existing applications. To address these problems Microsoft developed a platform called .NET.
The alpha version of it was released in the mid of year 2000. Also in the same year Microsoft announced their C# language as a de fact language of the .NET platform. The C# compiler is considered to be the most efficient compiler in the .NET family and a major part of the .NET base classes libraries itself are written in C#.
1.3 Characteristics of C#
The major characteristics of C# are:
Simple C# was designed to be easy for the professional programmer to learn and use effectively. If you have some programming experience, you will not find C# hard to master. Also, some of the confusing concepts from C++ are either left out of C# or implemented in a clearer manner.
Say for example the complex pointers in C++ are missing in C#. In C++ you have ::, . , -> operators for namespaces, member access and references respectively. But in C# a single dot ( . ) operator does all these operations. Modern The C# is emerged as a language for writing NGWS (Next Generation Windows Services) applications.
In C# memory management is automatic and is no longer a responsibility of the programmers. This automatic memory management is a responsibility of C# garbage collector. Another modern feature of C# is that it support cross language exception handling. A new data type called decimal is added in C# for monetary calculations. Another modern feature of C# is that it support a robust security model. Object-oriented C# supports major object oriented programming features such as data encapsulation, inheritance and polymorphism.
It contains keywords for declaring new classes and their methods and properties and for implementing encapsulation, inheritance and polymorphism. Versionable Versioning is the process of evolving a component over time in a compatible manner. A new version of a component is source compatible with a previous version if the code that depends on the previous version can, when recompiled, work with the new version. With versioning support in place, a C# developer can guarantee that as his class library evolves, it will remain binary compatibility with existing client applications. Compatible The .NET common language specification defines a standard for interoperation between .NET languages. The C# language provides support for access to standard COM objects OLE Automation.
The C# programs can natively use existing COM objects, irrespective of the language they are written in. Finally it enables you to interoperate with C-style APIs. Flexible In C# code can be written either in safe mode or in unsafe mode. The default for C# code is safe mode which does not support pointers. But you can declare certain classes or certain methods of classes to be unsafe which enables you to use pointers. Interoperable C# provides support for cross language interoperability. Interoperable C# provides support for cross language interoperability.
The code produced by C# can work easily with the code produced by other languages. This is made possible through the CLS, which defines a set of rules that every language in the .NET framework has to follow. The C# programs can natively use existing COM objects, irrespective of the language they are written in. Type safe The Common Type System provides type safety, which in turn improves code stability. It takes the following type safety measures: o You cannot use uninitialized variables. o It enforces overflow checking in arithmetic operations. o It does bounds checking in case of arrays. o Reference parameters that passed are type-safe
1.4 Applications of C#
The C# language can be used for a variety of applications that are supported by the .NET platform. o Reference parameters that passed are type-safe
1.4 Applications of C#
The C# language can be used for a variety of applications that are supported by the .NET platform.
Some of these applications include: Console application Windows applications Developing windows controls Developing ASP.NET projects Creating Web controls Providing Web services Developing .NET component library
1.5 Difference between C++ and C#
Following are the differences between C# and C++.
C# | C++ |
| Code can be written either in safe mode or in unsafe mode. The default for C# code is safe mode which does not support pointers where as the other mode (unsafe mode) support pointers. | Pointers are a prominent feature of C++ |
| There is no numeric equivalent for true and false. | In C++, any non-zero number means true and zero means false. | C++ supports it. |
| It detects uninitialized variables and generates compilation error. | Does not detect uninitialized variables and results unpredictable results. |
| It supports foreach loop | It does not support foreach loop |
| In C#, we cannot access static members via an object. | In C++, we can access static members via an object. |
| The switch expression can be a string | Strings are not allowed as switch expression. |
| In switch case statement you cannot fall through to the next case if the case statement is not empty. |
| In switch case statement you cannot fall through to the next case if the case statement is not empty. So it requires an explicit jump statement at the end of each case statement. | In switch case statement fall through from one case to the next case is allowed even if the case statement is not empty. |
| Does not support multiple inheritance | Very well support multiple inheritance. |
| Permits declaration of variable between goto and label. | Does not permit it |
| All data types in C# are inherited from object super class and therefore they are objects | Primitive data types are not objects. | Variable names can be duplicated in different code blocks of a routine. |
| Special keyword override is used to override a base class method | No special keyword for method overriding |
| The types of parameters that can be passed to a method are in, out and ref. |