Braintique.com header
Left Navigation Bar

Explicit Type Conversion

As I stated in the first part of this article, explicit conversion is denoted using the cast operator, which is the name of the type being converted to inside parentheses placed before the expression being converted. For example

int I = (int) L;

converts the long value stored in the variable L to an integer value store in the variable I.

As I noted, an int value can be implicitly converted to a long value implicitly without any additional C# syntax while the reverse is not true: a long value cannot be implicitly converted to type int.

It is easy to see why this should be the case. Longs have a much larger range of possible values than ints, and there is no way the compiler can know that a long-to-int conversion won’t occur when the long stores a bigger value than the int can store, causing a run-time error. In other words, the conversion isn’t guaranteed safe.

If you change the code in the example around, to attempt to cause an implicit conversion by calling a method with an long argument that is expecting an int:

int doubleIt(int inNum) {
return inNum * 2;
}
...
long X = 2;
MessageBox.Show(doubleIt(X).ToString());

it won’t compile, and you’ll get a syntax error.

syntax error

However, sometimes programmers do, in fact, know more than compilers. It might make sense for you, the all-knowing programmer, to want to convert the long value to an int. You might know perfectly well that the variable holding the long value in the program will never hold a big enough number at the point it is converted to int to cause a problem.

In this case, you’d use an explicit conversion by placing a cast operator in front of the expression to be converted. A cast operator is the name of the type being converted to inside parentheses. For example, to convert the long variable X to an integer variable Y:

int Y = (int) X;

Using this cast in the example that gave us a syntax error, it will compile and run fine now:

int doubleIt(int inNum) {
return inNum * 2;
}
...
long X = 2;

// cast the long value to an int
int Y = (int) X;

// now it displays 4
MessageBox.Show(doubleIt(Y).ToString());

By the way, of course you don’t really need a separate statement for the performance of the cast; it can be done at the same time as the method invocation. The doubleIt method, which expects an int argument, receives it in the form of a cast from long to int:

...
long X = 2;
MessageBox.Show(doubleIt((int)X).ToString());

The table below shows the explicit numeric conversions that you can do in C# using casting. As you might expect, it is perfectly permissible to explicitly cast a type to another even if the conversion could be handled by implicit conversion (for example, int to long).

Table: Permissible Explicit Numeric Conversions

Type (Conversion From)

Legal Explicit Conversion To

sbyte

byte, ushort, uint, ulong, or char

byte

sbyte or char

short

sbyte, byte, ushort, uint, ulong, or char

ushort

sbyte, byte, short, or char

int

sbyte, byte, short, ushort, uint, ulong, or char

uint

sbyte, byte, short, ushort, int, or char

long

sbyte, byte, short, ushort, int, uint, ulong, or char

ulong

sbyte, byte, short, ushort, int, uint, long, or char

char

sbyte, byte, or short

float

sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal

double

sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal

decimal

sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double

Casts can be done between more complex reference types as well as simple numeric types, of course. Some rules do apply. For example, object can be cast to any reference type, and a base class can be explicitly converted to a derived class.

For example, if you have a bunch of controls wired up to fire one event, you can cast the sender parameter to the Control type:

Control ctrl = (Control) sender;

You can then use the Name property of the Control variable to display which control fired the event:

private void button1_Click(object sender, System.EventArgs e){
Control ctrl = (Control) sender;
MessageBox.Show (ctrl.Name + " invoked the event!",
"A cast today the explicit way...");
}

we know what control fired the event

I knew that this cast would not fail, because each object stored in the sender parameter had to be a control derived from the Control class.

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write