C# Self Tutor (Chapter 1)
GETTING STARTED
C# is pronounced as “C sharp”. It is a new programming language that enables programmers in quickly building solutions for the Microsoft .NET platform
C# is a java like programming language intended for writing enterprise applications. The C# is an evolution of C and C++. It uses many C++ features in the areas of statements, expressions and operators. C# introduces considerable improvement and innovations in areas such as type safety, versioning, events and garbage collection.
We assume that we have no prior knowledge of any programming language. But before we get ensnared in the fascinating world of C#, let’s make a directory where we will save all our work. In order to do so, click on Start – Programs then go to Accessories and select Command Prompt in windows 2000 or the MS-DOS prompt as it is called in windows 98. Once at the command prompt create a directory called csharp (md csharp) and change to this directory (cd csharp). Now type the command ‘edit ebony.cs’, which will open the MS-DOS editor.
C:\csharp>edit ebony.cs
Here .cs is used as the extension used for C# files.
NEED FOR A NEW TECHNOLOGY:
Note that C# provides access to the common API styles: .NET, .COM, Automation and C-style API’s. it also supports unsafe mode, where you can use pointers to manipulate memory that is not under the control of the garbage collector.
C# is an elegant, simple, type safe, object oriented language that allows enterprise programmers to build a breadth of applications. C# also gives you the capability to build durable system-level components by virtue of the following features:
>Full COM/Platform support for the existing code integration.
>Robustness thru garbage collection and type safety.
>Security provided thru intrinsic code trust mechanisms
>Full support of extensible metadata concepts.
As the number of lines of code increases in a program it becomes very much difficult ton manage the program. The C++ programming language helps in managing the code by introducing the classes. The code maintainability has further been simplified by introducing software development with the help of COM (Component Object Model). The COM components can be developed by using different languages and tools provided by Microsoft. But developing a COM component was never an easy task. Hence there is always a demand for a language/platform which can help in developing components in a simpler way.
When the number of application grows in a traditional windows operating system there are always chances of a dll (dynamic linked library) of one application may be overwritten by a dynamic linked library of another application or a new version of same application. Again different applications might use the same dll name. This results in crashes and even sometimes makes the operating system unstable.
In order to tackle all these issues Microsoft has developed the .NET platform. This modern platform helps in fast development of good quality software which is easier to maintain. The applications developed under this platform are now can be distributed, in nature and can be accessed thru internet using standard in internet protocols.
COMMON LANGUAGE RUNTIME (CLR)
The common language runtime is the core of the .NET framework. It provides the runtime environment for running different programming languages supported by .NET. The CLR supports cross language interoperability by supporting intermediate language (IL) and Common Type System (CTS). The code running under CLR is known as Managed Code. CLR also supports automatic garbage collection. When an object is not referenced the garbage collector automatically removes the memory used by that object. Thus a programmer is free from the burden of releasing the memory in .NET
CLASS LIBRARY:
The .NET class library is a huge collection of classes. These classes are referred in a .NET programming language to achieve different functions. The .NET programming language cannot do anything without these classes. These classes are available in different groups like input/output, networking, security, diagnostics and runtime, data and XML classes. Web services and web form classes and windows forms classes.
COMMON LANGUAGE SPECIFICATION:
The language interoperability is one of the most important features of the .NET framework platform. In order to achieve this common language specification (CLS) is defined. The language features supported across different languages are included in CLS. The language construct supports by one language and not supported by the other language is not included in CLS. For example the unsafe code is supported in C#, but not supported in VB. Thus pointers are not CLS obedient.
CHARACTERISTICS OF C#:
The C# is a modern object oriented programming language which is derived from C and C++. The concepts C and C++ programming language are implemented in C# programming language in a simplified way. The key features of the C# programming language are:
1) Simple: C# achieves simplicity by eliminating the operators like à, :: which are used in C++. In C# all these operators are replaced by the dot (.) operators. In C++ the integers are sometimes used as Booleans. But in C# integers and Booleans are different data types. Bool type is an alias of the class System. Boolean and can have true and false values. In C# integers are never used for a true or false comparison. Such comparisons are done explicitly by comparing with zero or null.
2) Consistent: all the types are considered as objects in C#. The namespace helps in referring the base classes and all these classes can be accessed by using namespace. Even one can use his own namespace. The namespace of one program can be accessed in another program using simpler mechanism.
3)Modern: C# provided support for modern features like automatic garbage collection, rich error handling, decimal data type for financial and scientific calculations and modern delegate approaches. The automatic garbage collection helps in cleaning all the memory when memory is not referenced. C# has also throw, try …catch and try … finally blocks for handling errors and exceptions.
4) Object oriented: C# is an object oriented programming language and supports encapsulation, inheritance and polymorphism. Encapsulation is achieved by exposing the functional details to the user and hiding the internal details. The encapsulation is achieved by the use of the modifier like public, private or protected on different members of a class. In C# the methods are not virtual by default. The methods can be redefined with the help of virtual and override keywords. The concepts of global function and constants are eliminated in C#.
5) Type safety: In C# all the types are initialized tom their default values. The range of the arrays is always checked. The array in C# is an object. It is impossible to create an invalid cast in C#. All the memory referenced is cleared by automatic garbage collection.
6) Version able: C# provides support for versioning by using the new and overrides keywords. In the derived class a method name can be declared same as the base class method and with the new modifier. A method in the base class can also be declared with the virtual keyword and can be overridden in the derived class by the applying the override modifier against the method.
7) Compatible: one can invoke windows API and classic COM components in a C# program. C# also provides support for all the data types of standard COM component.
8) Flexible: uses of pointers are not encouraged in C#. However one can use the pointers by declaring the class with unsafe keyword. However these methods will not be type safe and will work with the managed space. One can also pin down the objects so that the garbage collection will not relocate them and easy access can be achieved by the pointers.
C# a C++ PERSPECTIVE:
C# is derived from C and C++. The C# has many syntax and concepts which are similar to C++.
>In C++ the main method can have integer return type. But in C# the Main method have void and integer return type.
>The members of a class in C# are defined inside the class definition. A member in C++ can be defined outside class definition.
>A class definition ends with a semicolon in C++. However in C# s semicolon is not used at the end of the class definition.
>All the classes in C# are derived from the super class object.
>In C# data types can be value types are reference types( class, interfaces, delegates)
>In C# all the variables are initialized with a default value. There are chances of un initialized variables in C++.
>C# supports string types and the string class provided lots of methods for easy manipulation of the strings.
>In C# the switch statement expression can be of a string type and does not allow automatic fall thru like C++.
>C# does not support multiple inheritances.
>All casting operations in C# are type safe.
C# a JAVA PERSPECTIVE:
Java is the most popular language for developing web enabled applications.
>The java compiler generates the byte code and the Java Virtual Machine interprets the instructions. In C# the compiler produces Intermediate Language (IL) which then runs under the CLR. The Just in Time compiler then converts the IL code to native language.
>C# defines unsigned and signed data types. There are no unsigned data types in java.
>In C# all data types are considered as an object.
>C# uses the const keyword to declare constants. Java uses final keyword to declare constants.
>In C# it is possible that different class can have main method. During the compilation one can specify which main method will be used as entry point.
>In C# the namespace is used to refer to a group of classes. Java uses the packages keyword for similar purposes. In java the packages are linked with the directory structure.
>C# supports structure data type.
>Java handles all garbage collection automatically. In C# also garbage collection is automatic. However there is a provision to write destructors in C# programming language.
>Java uses inner class and interferences for handling events. C# uses delegates for event handling.
>C# uses “is” operator to check the type at runtime. Java has “instanceof” operator for the same.
