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.

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();
}
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
pghtech
  • 3,642
  • 11
  • 48
  • 73
  • I don't get it... `I can't seem to look up, because I don't know what it is called ... It seems like casting to me`. Did you try searching for `c# casting`? I also tried searching `type parentheses` and the 2nd and 3rd results address it. – Nelson Rothermel Jul 02 '10 at 14:18
  • I just tried your suggestion (which were the same results as my query - and no, I am not seeing how they address what I am asking. – pghtech Jul 02 '10 at 14:24

5 Answers5

6

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

Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • 1
    Specifically, it is explicit casting, (vs implicit casting). Explicit: http://msdn.microsoft.com/en-us/library/xhbhezf4(v=VS.80).aspx Implicit: http://msdn.microsoft.com/en-us/library/z5z9kes2(v=VS.80).aspx – David Jul 02 '10 at 14:10
  • 8
    @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 Jul 02 '10 at 14:12
  • @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. – jyoungdev Jul 02 '10 at 14:15
  • @Eric thanks for the clarification! PS: I love reading your blog. – David Jul 02 '10 at 17:06
4

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
  • 7,365
  • 9
  • 47
  • 77
  • This is correct. However, what is confusing me is that I guess I am expecting the numericUpDown1.Value to 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 Jul 02 '10 at 14:45
  • 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 Jul 02 '10 at 14:51
2

You're right. It is casting.

DarLom
  • 1,100
  • 2
  • 12
  • 30
2

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,204
  • 21
  • 24
1

As Eric Lippert (one of the designers of C#) mentioned in a comment elsewhere, this is Conversion - converting from one type to another.

It's not Casting. Casting is a leftover concept from C, where sometimes the compiler didn't know what type something was. The cast was a message to the compiler how to interpret the variable.

Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
  • 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 Jul 02 '10 at 14:54
  • 3
    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 Jul 02 '10 at 14:54
  • 1
    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 Jul 02 '10 at 14:57
  • 6
    This is a great candidate for my "what's the difference?" blog series. :-) – Eric Lippert Jul 02 '10 at 14:59
  • 2
    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 Jul 02 '10 at 15:00
  • @EricLippert, i don't know if you really did a blog post for this, but apparently i can't see it in the ["what's the difference?"](http://ericlippert.com/category/whats-the-difference/) series. Or did you decide to not do it anyways? – aiapatag Jun 06 '13 at 10:59
  • @EricLippert My pleasure. It's because I'm sure that most of your articles cause Eureka effect to a lot of people, including me, of course. That's why I was curious when I found this post. IMO, it's a very interesting topic. – aiapatag Jun 06 '13 at 15:19