The .Net Common Language Specifications
In an object-oriented environment such as C# and the .Net platform, everything is an object. Once an object is created it needs to be able to communicate or interact with other objects, and the object may need to be manipulated.
These other objects may have been created in a different language, for example, C++. Other languages such as C++ define data types differently (a good example is a string, which in C++ is a pointer to a null-terminated character array). If you were to pass a string to that external method you would need to convert your C# string object into a pointer to an array of characters which has been terminated with a null character.
This approach can get complicated, and sometimes many type conversions are required. This not only leads to messy code but also causes an unwanted performance overhead and a lapse in concentration by the programmer can lead to unwanted memory leaks and unstable applications.
Common Type System
The .Net platform overcomes this problem by implementing the Common Type System (CTS). The Common Type System means that every language in .Net uses the same data types (or objects). When you declare a string in C# you are creating an instance of a string class. When you define a string in Visual Basic, you create an instance of the same string class, thus when you pass parameters between languages there is no longer any requirement to convert data types.
In the .Net framework types are split into two different categories: value types and reference types.
A value type directly contains its data. A variable of type int contains a value (e.g. 12345) and only a value. Each variable of a value type has its own copy of the data. An operation on one variable does not affect another. Value types are stored in a memory location known as the stack.
Reference types contain a reference, or memory location, of their data. The data is stored as an object on the heap. Unlike value types, two or more reference types can point to the same memory location, thus an operation on one variable can affect another. Reference types are instantiated with the new keyword.
Base Class Library
Another feature of the Common Language Specifications is that of the Base Class Library. The Base Class Library provides a common set of classes that all .Net languages can utilise. These classes range from a standard Windows form class to classes for temperature, coordinates, data access, XML parsers and so on.
The Base Class Library is divided up into namespaces so that they are easily navigable. For example, the SqlConnection class is located in the
System.Data.SqlClient namespace. By using this namespace C#, VB, C++ J# and so on can all interact with the same SqlConnection class, all access the same properties and methods.
Last updated on: Friday 23rd June 2017
There are no comments for this post. Be the first!