tags:

views:

421

answers:

5

I am going through a book on C# and have come across something that I can't seem to look up, because I don't know what it is called, or trying to search for something by description.

Could some explain to me what is going on, or the meaning behind the "(type)" that comes before a reference to an object as in (int)objectname.variablename?

It seems like casting to me.

Thanks.

EDIT: Since most of you are going off 'My' reference to casting when I am only guessing, and needed more code, I am including the code that I am reviewing that has brought on this question. I am questioning the (int) in the (int)numericupDown1.Value;

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
    dinnerParty.NumberOfPeople = (int)numericUpDown1.Value;
    DisplayDinnerPartyCost();
}
+6  A: 

It is casting, it is trying to cast variablename into type (type) at runtime

w69rdy
Specifically, it is explicit casting, (vs implicit casting). Explicit: http://msdn.microsoft.com/en-us/library/xhbhezf4(v=VS.80).aspxImplicit: http://msdn.microsoft.com/en-us/library/z5z9kes2(v=VS.80).aspx
David
@David: No, it is explicit *conversion*. You are confusing explicit and implicit casting with explicit and implicit *conversion*. Implicit *casts* are extremely rare in C#; the only time people commonly come across an implicit use of the cast operator is in the foreach loop,.
Eric Lippert
@pghtech, if you've done "C-style type casting" in C or C++, or casting in Java, then you'll be fairly familiar with this already. I'm not enough of a language lawyer to guarantee identical semantics, but C# casting is similar to that of Java.
apollodude217
@Eric thanks for the clarification! PS: I love reading your blog.
David
+2  A: 

You're right. It is casting.

Darvis Lombardo
+2  A: 

Without knowing the types involved it's hard to say exactly what is happening.

Casting will explicitly invoke the conversion operator from one type to another: http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx

It could however be unboxing or explicit boxing. Boxing/Unboxing is an expensive way of storing value types on the heap. http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx

matt-dot-net
+4  A: 

It is casting(converting) the numericUpDown1.Value to an int value. This can also be done using

Convert.toInt32(numericUpDown1.Value)

I'm assuming that dinnerParty.NumberOfPeople is an integer value and therefore if you didn't cast it would throw an error if the value supplied was not an integer. For example if the Value supplied was a double or a string it would throw an error, with casting it would convert the double say 20.5 to 20 and it would be accepted. For the string it would depend on if the string contained a number in it. If the string was "12" then using the convert method mentioned above would convert it to the integer 12.

Gage
This is correct. However, what is confusing me is that I guess I am expecting the numericUpDown1.Valueto be a int. But I see reading the MSDN "http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.value.aspx" description, that Value is of decimal type. http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.increment.aspx
pghtech
After looking at the MSDN you are correct it would return an integer value. I was thinking more along the lines of http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvalue.aspx
Gage
+1  A: 
Joel Spolsky
I agree that it's more of a leftover term, but it's one the C# programmer's guide still uses. From the link I included in my answer: "Converting between data types can be done explicitly using a cast, but in some cases, implicit conversions are allowed."
matt-dot-net
No, the original poster is in fact casting. The difference I'm attempting (and apparently failing) to call out here is that casting is *syntactic*, it is the use of a *cast operator*. Conversion is *semantic*, it is an operation in a type algebra. The (syntactic) cast indicates that the user wishes some conversion to occur. That conversion can be an implicit conversion or an explicit conversion, and can be representation preserving or representation changing.
Eric Lippert
Normally a cast, being a syntactic element, appears in the code directly. However, there are some rare situations in which the compiler will silently insert a cast operator on your behalf to perform an explicit conversion. The most common one is "foreach(Giraffe g in myAnimals)" - many people assume this to be equivalent to "filter out the tigers", but in fact it means "put a cast operator on each thing that comes out and throw an exception if the explicit conversion fails."
Eric Lippert
This is a great candidate for my "what's the difference?" blog series. :-)
Eric Lippert
ok, i'm totally confused now. I thought there was a distinction between what we would have, in the old days, called a "reinterpret cast" and an actual conversion. I [opened a new question](http://stackoverflow.com/questions/3166840/what-is-the-difference-between-casting-and-conversion)
Joel Spolsky