Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Functions of the Common Type System  





2 Type categories  



2.1  Boxing and unboxing  





2.2  Boxing  





2.3  Unboxing  







3 See also  





4 References  





5 External links  














Common Type System






Français


Русский
Svenska
Türkçe
Tiếng Vit

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


In Microsoft's .NET Framework, the Common Type System (CTS) is a standard that specifies how type definitions and specific values of types are represented in computer memory. It is intended to allow programs written in different programming languages to easily share information. As used in programming languages, a type can be described as a definition of a set of values (for example, "all integers between 0 and 10"), and the allowable operations on those values (for example, addition and subtraction).

The specification for the CTS is contained in Ecma standard 335, "Common Language Infrastructure (CLI) Partitions I to VI." The CLI and the CTS were created by Microsoft, and the Microsoft .NET framework is an implementation of the standard.

Functions of the Common Type System

[edit]

When rounding fractional values, the halfway-to-even ("banker's") method is used by default, throughout the Framework. Since version 2, "Symmetric Arithmetic Rounding" (round halves away from zero) is also available by programmer's option.[1]

Type categories

[edit]

The common type system supports two general categories of types:

Value types
Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations.
Reference types
Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. The type of a reference type can be determined from values of self-describing types. Self-describing types are further split into arrays and class types. The class types are user-defined classes, boxed value types, and delegates.

The following example written in Visual Basic .NET shows the difference between reference types and value types:

Imports System

Class Class1
    Public Value As Integer = 0
End Class 'Class1
 
Class Test
    Shared Sub Main()
        Dim val1 As Integer = 0
        Dim val2 As Integer = val1
        val2 = 123
        Dim ref1 As New Class1()
        Dim ref2 As Class1 = ref1
        ref2.Value = 123
        Console.WriteLine("Values: {0}, {1}", val1, val2)
        Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value)
    End Sub 'Main
End Class 'Test

The output of the above example

Values: 0, 123
Refs: 123, 123

Boxing and unboxing

[edit]

Boxing

[edit]

Converting value types to reference types is also known as boxing. As can be seen in the example below, it is not necessary to tell the compiler an Int32 is boxed to an object, because it takes care of this itself.

Int32 x = 10; 
object o = x ; // Implicit boxing
Console.WriteLine("The Object o = {0}",o); // prints out "The Object o = 10"

However, an Int32 can always be explicitly boxed like this:

Int32 x = 10; 
object o = (object) x; // Explicit boxing
Console.WriteLine("The object o = {0}",o); // prints out "The object o = 10"

Unboxing

[edit]

The following example intends to show how to unbox a reference type back to a value type. First an Int32 is boxed to an object, and then it is unboxed again. Note that unboxing requires explicit cast.

Int32 x = 5; 
object o1 = x; // Implicit Boxing
x = (int)o1; // Explicit Unboxing

See also

[edit]

References

[edit]
  1. ^ "MidpointRounding Enumeration". Microsoft Docs.
[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Common_Type_System&oldid=1139141512"

Category: 
Common Language Infrastructure
Hidden categories: 
Wikipedia articles that are too technical from August 2009
All articles that are too technical
 



This page was last edited on 13 February 2023, at 15:58 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki