C# Programming: Common Type System

8:00:00 PM
Common Type System in C# Programming
C# has a unified kind system. This unified kind system is called Common Type System (CTS).

A unified type system implies that all types, including primitives such as integers, are sub-classes of the System.Object class. For example, every type inherits a  ToString() method.



Categories of data types


CTS separates data types into two categories:
• Value types
• Reference types

Instances of valuable types don't have referential identity nor referential comparison linguistics - equality and inequality comparisons for value types compare the particular data values inside the instances, unless the corresponding operators are overloaded. Value types are derived from System.ValueType, always have a default value, and can always be created and copied. Another limitations on value types are that they can't derive from one another (but can implement interfaces) and can't have a definite default (parameterless) constructor. Examples of value types are all primitive types, such as int (a signed 32-bit integer), float (a 32-bit IEEE floating-point number), char (a 16-bit Unicode code unit), and System.DateTime (identifies a specific point in time with nanosecond precision). Other examples are enum (enumerations) and struct (user defined structures).

In distinction, reference types have the notion of referential identity - every instance of a reference type is inherently distinct from each different instance, even if the data within both instances is the same. This can be mirrored in default equality and inequality comparisons for reference types, which test for referential instead of structural equality, unless the corresponding operators are overloaded (such as the case for System.String). In general, it's not always possible to make an instance of a reference type, nor to copy an existing instance, or perform a value comparison on two existing instances, although specific reference types will offer such services by exposing a public constructor or implementing a corresponding interface (such as ICloneable or IComparable). Examples of reference types are object (the ultimate base class for all other C# classes), System.String (a string of Unicode characters), and System.Array (a base class for all C# arrays).
Both type categories are extensible with user-defined types.


Boxing and Unboxing



Boxing is that the operation of converting a value-type object into a value of a corresponding reference type. Boxing in C# is implicit.

Unboxing is the operation of converting a value of a reference type (previously boxed) into a value of a value type. Unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T).

For Example :

int foo = 42;// Value type.
object bar = foo;     // foo is boxed to bar.
int foo2 = (int)bar;  // Unboxed back to value type.

Generics

Generics were value-added to version 2.0 of the C# language. Generics use type parameters, that create it possible to design classes and methods that do not specify the type used until the class or method is instantiated. The main advantage is that one can use generic type parameters to create classes and methods that can be used without incurring the cost of runtime casts or boxing operations.


For Example:

// Declare the generic class.
 
public class GenericList<T>
{
    void Add(T input) { }
}
 
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();
 
        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();
 
        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}

Share this :

Previous
Next Post »
0 Komentar

Please comment on topic