Microsoft.NET

……………………………………………….Expertise in .NET Technologies

C# Data Types and Casting

Posted by Ravi Varma Thumati on February 12, 2015

Nullable Types

In C# types are divided into 2 broad categories

  • Value Types – int, float, double, structs, enums etc
  • Reference Types– Interface, Class, delegates, arrays etc

By default value types are non nullable. To make them nullable use ?

int i = 0 (i is non nullable, so “i” cannot be set to null, i = null will generate compiler error)

int? j = 0 (j is nullable int, so j=null is legal)

Null Coalescing Operator ?? 

Program without using NULL coalescing operator

using System;

class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;

if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}

Console.WriteLine(“Available Tickets={0}”, AvailableTickets);
}
}

The above program is re-written using NULL coalescing operator

using System;

class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;

//Using null coalesce operator ??
AvailableTickets = TicketsOnSale ?? 0;

Console.WriteLine(“Available Tickets={0}”, AvailableTickets);
}
}

Data Type Conversions:

  1. Implicit conversions
  2. Explicit Conversions
  3. Difference between Parse() and TryParse()

Implicit conversion is done by the compiler:

  • When there is no loss of information if the conversion is done
  • If there is no possibility of throwing exceptions during the conversion

Example: Converting an int to a float will not lose any data and no exception will be thrown, hence an implicit conversion can be done.

Whereas when converting a float to an int, we lose the fractional part and also a possibility of overflow exception. Hence, in this case an explicit conversion is required. For explicit conversion we can use cast operator or the convert class in c#.

Implicit Conversion Example

using System;
class Program
{
public static void Main()
{
int i = 100;

// float is bigger datatype than int. So, no loss of
// data and exceptions. Hence implicit conversion
float f = i;

Console.WriteLine(f);
}
}

Explicit Conversion Example

using System;
class Program
{
public static void Main()
{
float f = 100.25F;

// Cannot implicitly convert float to int.
// Fractional part will be lost. Float is a
// bigger datatype than int, so there is
// also a possiblity of overflow exception
// int i = f;

// Use explicit conversion using cast () operator
int i = (int)f;

// OR use Convert class
// int i = Convert.ToInt32(f);

Console.WriteLine(i);
}
}

Difference between Parse and TryParse

  • If the number is in a string format you have 2 options – Parse() and TryParse()
  • Parse() method throws an exception if it cannot parse the value, whereas TryParse() returns a bool indicating whether it succeeded or failed.
  • Use Parse() if you are sure the value will be valid, otherwise use TryParse()

This tip presents the main difference between int.Parse(), Convert.ToInt32() and int.TryParse().

int.Parse(string s)

Simply, int.Parse (string s) method converts the string to integer. If string s is null, then it will throw ArgumentNullException.

If string s is other than integer value, then it will throw FormatException. If string s represents out of integer ranges, then it will throw OverflowException.

Example

//// int.Parse(string s)//

class Program

{

static void Main(string[] args)

{

string str1=”9009″;

string str2=null;

string str3=”9009.9090800″;

string str4=”90909809099090909900900909090909″;

int finalResult;

finalResult = int.Parse(str1); //success

finalResult = int.Parse(str2); // ArgumentNullException

finalResult = int.Parse(str3); //FormatException

finalResult = int.Parse(str4); //OverflowException

}

}

Convert.ToInt32(string s)

Simply, Convert.ToInt32(string s) method converts the string to integer. If string s is null, then it will return 0 rather than throw ArgumentNullException. If string s is other than integer value, then it will throw FormatException. If string s represents out of integer ranges, then it will throwOverflowException.

Example

//

// Convert.ToInt32(string s)

//

class Program

{

static void Main(string[] args)

{

string str1=”9009″;

string str2=null;

string str3=”9009.9090800″;

string str4=”90909809099090909900900909090909″;

int finalResult;

finalResult = Convert.ToInt32(str1); // 9009

finalResult = Convert.ToInt32(str2); // 0

finalResult = Convert.ToInt32(str3); //FormatException

finalResult = Convert.ToInt32(str4); //OverflowException

}

}

int.TryParse(string s,Out)

Simply int.TryParse(string s,out int) method converts the string to integer out variable and returnstrue if successfully parsed, otherwise false. If string s is null, then the out variable has 0 rather than throw ArgumentNullException. If string s is other than integer value, then the out variable will have 0rather than FormatException. If string s represents out of integer ranges, then the out variable will have 0rather than throw OverflowException.

Example

//

// Convert.ToInt32(string s)

//

class Program

{

static void Main(string[] args)

{

string str1=”9009″;

string str2=null;

string str3=”9009.9090800″;

string str4=”90909809099090909900900909090909″;

int finalResult;

bool output;

output = int.TryParse(str1,out finalResult);

                      // 9009

output = int.TryParse(str2,out finalResult);

                     // 0

output = int.TryParse(str3,out finalResult);

                    // 0

output = int.TryParse(str4, out finalResult);

                   // 0

}

}

Finally, among all these methods, it is better to use int.TryParse(string s, out int) as this method is the best forever.

Finally, It is recommend to use int.TryParse(string s, out int).

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 CLR boxes a value type, 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 the 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.

int i = 123;

// The following line boxes i.

object o = i;

The object o can then be unboxed and assigned to integer variable i:

o = 123;

i = (int)o;  // unboxing

The following examples illustrate how boxing is used in C#.

// String.Concat example.

// String.Concat has many versions. Rest the mouse pointer on

// Concat in the following statement to verify that the version

// that is used here takes three object arguments. Both 42 and

// true must be boxed.

Console.WriteLine(String.Concat(“Answer”, 42, true));

// List example.

// Create a list of objects to hold a heterogeneous collection

// of elements.

List<object> mixedList = new List<object>();

// Add a string element to the list.

mixedList.Add(“First Group:”);

// Add some integers to the list.

for (int j = 1; j < 5; j++)

{

// Rest the mouse pointer over j to verify that you are adding

// an int to a list of objects. Each element j is boxed when

// you add j to mixedList.

mixedList.Add(j);

}

// Add another string and more integers.

mixedList.Add(“Second Group:”);

for (int j = 5; j < 10; j++)

{

mixedList.Add(j);

}

// Display the elements in the list. Declare the loop variable by

// using var, so that the compiler assigns its type.

foreach (var item in mixedList)

{

// Rest the mouse pointer over item to verify that the elements

// of mixedList are objects.

Console.WriteLine(item);

}

// The following loop sums the squares of the first group of boxed

// integers in mixedList. The list elements are objects, and cannot

// be multiplied or added to the sum until they are unboxed. The

// unboxing must be done explicitly.

var sum = 0;

for (var j = 1; j < 5; j++)

{

// The following statement causes a compiler error: Operator

// ‘*’ cannot be applied to operands of type ‘object’ and

// ‘object’.

//sum += mixedList[j] * mixedList[j]);

// After the list elements are unboxed, the computation does

// not cause a compiler error.

sum += (int)mixedList[j] * (int)mixedList[j];}

// The sum displayed is 30, the sum of 1 + 4 + 9 + 16.

Console.WriteLine(“Sum: ” + sum);

// Output:

// Answer42True

// First Group:

// 1

// 2

// 3

// 4

// Second Group:

// 5

// 6 // 7 // 8 // 9

// Sum: 30

Performance

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. To a lesser degree, the cast required for unboxing is also expensive computationally.

Boxing

Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.

Consider the following declaration of a value-type variable:

int i = 123;

The following statement implicitly applies the boxing operation on the variable i:

// Boxing copies the value of i into object o. object o = i;

The result of this statement is creating an object reference o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.

Boxing Conversion

It is also possible to perform the boxing explicitly as in the following example, but explicit boxing is never required:

C#

int i = 123;object o = (object)i;  // explicit boxing

Description

This example converts an integer variable i to an object o by using boxing. Then, the value stored in the variable i is changed from 123 to 456. The example shows that the original value type and the boxed object use separate memory locations, and therefore can store different values.

Example

class TestBoxing

{

static void Main()

{

int i = 123;

// Boxing copies the value of i into object o.

object o = i;

// Change the value of i.

i = 456;

// The change in i does not effect the value stored in o.

System.Console.WriteLine(“The value-type value = {0}”, i);

System.Console.WriteLine(“The object-type value = {0}”, o);

}

}

/* Output:    The value-type value = 456    The object-type value = 123*/

The following example demonstrates a case of invalid unboxing and the resulting InvalidCastException. Using try and catch, an error message is displayed when the error occurs.

class TestUnboxing

{

static void Main()

{

int i = 123;

object o = i;  // implicit boxing

try

{

int j = (short)o;

// attempt to unbox

System.Console.WriteLine(“Unboxing OK.”);

}

catch (System.InvalidCastException e)

{

System.Console.WriteLine(“{0} Error: Incorrect unboxing.”, e.Message);

}

}

}

This program outputs:

Specified cast is not valid. Error: Incorrect unboxing.

If you change the statement:

int j = (short) o;

to:

int j = (int) o;

the conversion will be performed, and you will get the output:

Unboxing OK.

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.

The following statements demonstrate both boxing and unboxing operations:

int i = 123;

// a value type object o = i;

// boxing int j = (int)o;

// unboxing

The following figure demonstrates the result of the previous statements.

Unboxing Conversion

For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox null causes a NullReferenceException. Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.

 

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: