0% found this document useful (0 votes)
7 views

Mastering C# Up and Down (4-June-2024)

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Mastering C# Up and Down (4-June-2024)

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Strings and string literals

You don't use the new operator to create a string object


except when initializing the string with an array of chars.
1. Initialize a string with the Empty constant value to create
a new String object whose string is of zero length.
2. The string literal representation of a zero-length string is
"".
3. By initializing strings with the Empty value instead
of null, you can reduce the chances of
a NullReferenceException occurring.
4. Use the static IsNullOrEmpty(String) method to verify the
value of a string before you try to access it.

Boxing and Unboxing


Boxing is the process of converting a value type to the
type object or to any interface type implemented by this
value type.
When the common language runtime (CLR) boxes a value
type, it wraps the value inside a System.Object instance 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 the type system in which a value of any type
can be treated as an object.
int i = 123; // The following line boxes i. object o = i;
o = 123; i = (int)o; // unboxing

the stack only allows items to be added and removed


to/from the top,
any item in a heap can be accessed at any time.
explicit boxing is never required:
int i = 123; object o = (object)i; // explicit boxing

Unboxing
Unboxing is an explicit conversion from the type object to
a value type or from an interface type to a value type that
implements the interface.
An unboxing operation consists of:
Checking the object instance to make sure that it is a
boxed value of the given value type.
Copying the value from the instance into the value-type
variable.
int i = 123; // a value type object o = i; // boxing int j =
(int)o; // unboxing

You might also like