Click to See Complete Forum and Search --> : Casting...


mshdsp
July 21st, 2008, 02:45 AM
Hi there,

I'm in the throws of learning C#, but I am currently trying to figure out why I would use casting with reference types. From what I understand, casting is used to convert one type to another (please correct me if I am wrong), and I understand this concept when using primitive data types. What I am struggling to understand is why you would use casting when dealing with reference types.

I've checked various sites, but none of them offer a clear understanding of why you would cast one object to another.

Am I missing something fundamental to OOP?

Any help would be gratefully received.

Tischnoetentoet
July 21st, 2008, 04:30 AM
Well, the variable (in C#, 99 % of the variables are a "pointer" to a referenced object) is of a specific object.

In C++, you also cast a pointer to a specific type if you want to call its functions. In C#, it works exactly the same. You need to convert the object to the right type so you can call the right functions on it.

mshdsp
July 21st, 2008, 04:41 AM
Well, the variable (in C#, 99 % of the variables are a "pointer" to a referenced object) is of a specific object.

In C++, you also cast a pointer to a specific type if you want to call its functions. In C#, it works exactly the same. You need to convert the object to the right type so you can call the right functions on it.

Many thanks for your response.

Do you have any recommended web-sources that give a thorough explanation?

Again, thanks for your help.

Arjay
July 21st, 2008, 10:29 AM
All types in C# derive from object. Some methods operate in a generic fashion and return an object that must be cast to the desired type.

For example, consider the System.Enum.Parse (http://msdn.microsoft.com/en-us/library/essfb559.aspx) method which returns an object. Given a string and an enum type, this method returns an object that can be cast to the enum type (with the string represented as an enum value).

enum Color { Red, Green, Blue };

Color color = (Color) Enum.Parse( typeof( Color ), "Green" );

// The value of color is 'Color.Green'.

In the above snippet, you would get an 'unable to convert type' error if you tried the following without the cast:
Color color = Enum.Parse( typeof( Color ), "Green" );

Read more in Casting (C# Programming Guide) (http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx).

As personal preference, I generally try to avoid casting as much as possible in my code. As such I rarely pass objects to and from methods.

RaleTheBlade
July 21st, 2008, 07:27 PM
As personal preference, I generally try to avoid casting as much as possible in my code. As such I rarely pass objects to and from methods.

How do you manage this in C# where everything derives from System.Object :confused: lol.

Also, C# has a nice little sugar syntax tweak that allows you to do this:


enum Color { Red, Green, Blue };

Color color = Enum.Parse( typeof( Color ), "Green" ) as Color;

// The value of color is 'Color.Green'.


Notice the "as Color" syntax on the end. I never use it, Im more partial to the C++ way but some developers might find it handy.

EDIT:

Oops, I forgot color was a VALUE type, therefore casting from a reference type to a value type does NOT work with the "as" syntax sugar. However, any reference type to reference type casts will work with it.

Arjay
July 21st, 2008, 07:40 PM
Originally Posted by Arjay
As personal preference, I generally try to avoid casting as much as possible in my code. As such, I rarely pass objects to and from methods.

How do you manage this in C# where everything derives from System.Object :confused: lol.Because I avoid passing objects in my methods.

For example, I tried to avoid doing this:
void Method( object obj );

If a method or class can operate on multiple types, I'll use generics.

Passing raw objects is always a last resort (just like I didn't like to pass void* in C++).

mshdsp
July 22nd, 2008, 12:23 AM
Thanks for the responses, guys. Very much appreciated! :thumb: