Skip to content

17. IClonable, Boxing, String value type/reference type

February 26, 2011

49. Explain current thinking about ICloneable.
The ICloneable Interface supports cloning, which creates a new instance of a class with the same value as the existing one. The ICloneable type exposes the method Clone() as a member. Clone() creates a new object that is a copy of the current instance, it goes beyond that supplied by MemberwiseClone. ICloneable is the prefered way to using copy constructors because it is standardized and utilized by other portions of the .NET Framework to generate object copies.

50. What is boxing?
Boxing is the process of converting a value type(Structs, enumerations, integral types, floating point types, decimals, booleans) to the type object or to any interface implemented by this value type. When the CLR boxes a value, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit, unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of type system, in which a value of any type can be treated as an object. In the following example, the integer variable i is boxed and assigned to object o:Boxing Sample
 The object o can then be unboxed and assigned to integer variable i:

UnBoxing Sample
Illustration of how boxing is used in C#:
Boxing and Unboxing Code Sample
In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. The cast required for unboxing is also expensive computationally. For more information see Performance in MSDN.

51. Is string a value type or reference type?
Value types consist of two main categories: Structs and Enumerations. Numeric types: Integral types, Floating-point types, decimal, bool and user defined structs. Variables based upon value types directly contain the values. Assigning one value type to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object and not the object itself.
Reference types, variables of reference types, refered to as objects, store references to the actual data. Keywords used to declare reference types: class, interface, delegate. Built-in reference types: dynamic, object, string. The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework. String is a reference type.

Happy Programming! =)

Source: Exclusively MSDN. Single Sourcing.

Advertisements

From → OOP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: