473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Help understand type casting

I'm trying to understand C# type casting. The following code will not
compile unless I Cast item to a String.

private static String myData;

foreach (DataRow row in MyTable.Rows)

{
foreach (Object item in row.ItemArray)
{
myData = item;
}
}

However when I use plus-equal instead of equal it compiles without casting
as shown in the code below.

foreach (DataRow row in MyTable.Rows)

{
foreach (Object item in row.ItemArray)
{
myData += item;
}
}

Can someone explain the difference?

Mar 17 '06 #1
2 1696
The issue isn't the casting per se as the + operator. It is performing
the conversion from object to string for you

e.g.

string str = "foo";
object obj = "bar";

string msg = obj; ' gives, cannot do an implicit
cast from object to string

string otherMsg = str + obj; ' works fine.
because the + operator adds the ToString() output from the object
e.g.

string str = "foo";
object obj = 17;

string otherMsg = str + obj; ' gives foo17

object obj = new Widget();

otherMsg = str + obj; ' gives fooMyNamespace.Widget
hth,
Alan.

Mar 17 '06 #2
In C# there are Primitive, Reference and Value type variables. In
particular, the primitive variables are variable that the compiler supports
directly such as String. Int, Double etc variables. This means that the
compiler has its own little rules on how to manage these primitive
variables.

In your example, the compiler automatically converted the object to a string
(ToString()). The compiler is set up to do that when you add a string with
something else that is not a string.

If you look at the definition of a string, you will notice that there are no
operator overload definition for operator such as +,-,*, division etc. as I
said before, the reason for this is because the compiler has its own set of
rules for this types of operation.

If you where to perform your example with one of your custom classes (where
you don't supply an operator overload for + that know how to handle adding a
string) the instruction were you add the variables will give you a compile
error. The reason for this is because internally, the compiler knows nothing
about how to perform operations with your own custom classes, you have to
tell the compiler what to do.
"keithb" <kb******@dslextreme.com> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
I'm trying to understand C# type casting. The following code will not
compile unless I Cast item to a String.

private static String myData;

foreach (DataRow row in MyTable.Rows)

{
foreach (Object item in row.ItemArray)
{
myData = item;
}
}

However when I use plus-equal instead of equal it compiles without casting
as shown in the code below.

foreach (DataRow row in MyTable.Rows)

{
foreach (Object item in row.ItemArray)
{
myData += item;
}
}

Can someone explain the difference?

Mar 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
2
by: mb | last post by:
got the following code snippet that i've modified off the internet to work - but damn if i know how and i dont like that. im having trouble with the following thread declaration that creates the...
4
by: kelli | last post by:
i am new to c# so if this is a trivial problem, forgive me! i've searched the web and after 2 days still cannot solve it. i am converting a c++ application to c# - the problem code is listed...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
9
by: Brian | last post by:
Hello! What is the proper syntax for casting? For example, how do I change an Integer in to a String if the variable is called Joe1 and has 20 assigned to it. Thanks, Brian
56
by: lnzju | last post by:
main(_){for(--_;putchar(_++-1););}
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.