ស៊ីទ្រុងជ្រូក: ប្រព័ន្ធប្រភេទសាមញ្ញ

ប្រព័ន្ធប្រភេទសាមញ្ញ 
C# has a unified type system. This unified type system is called Common Type System (CTS).

A unified type system implies that all types, including primitives such as integers, are subclasses of the  class. For example, every type inherits a  method.

 ចំណាត់ថ្នាក់ក្រុមប្រភេទទិន្នន័យ 
CTS separates data types into two categories:

  Value types
  Reference types

Instances of value types do not have referential identity nor referential comparison semantics - equality and inequality comparisons for value types compare the actual data values within the instances, unless the corresponding operators are overloaded. Value types are derived from , always have a default value, and can always be created and copied. Some other limitations on value types are that they cannot derive from each other (but can implement interfaces) and cannot have an explicit default (parameterless) constructor. Examples of value types are all primitive types, such as  (a signed 32-bit integer),  (a 32-bit IEEE floating-point number),  (a 16-bit Unicode code unit), and  (identifies a specific point in time with nanosecond precision). Other examples are  (enumerations) and  (user defined structures).

In contrast, reference types have the notion of referential identity - each instance of a reference type is inherently distinct from every other instance, even if the data within both instances is the same. This is reflected in default equality and inequality comparisons for reference types, which test for referential rather than structural equality, unless the corresponding operators are overloaded (such as the case for ). In general, it is not always possible to create an instance of a reference type, nor to copy an existing instance, or perform a value comparison on two existing instances, though specific reference types can provide such services by exposing a public constructor or implementing a corresponding interface (such as  or ). Examples of reference types are  (the ultimate base class for all other C# classes),  (a string of Unicode characters), and  (a base class for all C# arrays).

Both type categories are extensible with user-defined types.

 Boxing and unboxing 
Boxing is 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).

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 added to version 2.0 of the C# language. Generics use type parameters, which make 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, as shown here:

// 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>();
    }
}