3

I ran into a scenario today while implementing search functionality in my application that has left me puzzled. Check out this snippet:

public string GetFirstProductName(SortedList<string, object> itemsList) {
    for (int i = 0; i < itemsList.Values.Count; i++) {
        if (itemsList.Values[i] is Product)
           // Doesn't Compile:
           // return (Product)itemsList.Values[i].ProductName;

           // Does compile. Precedence for the "." higher than the cast?
           return ((Product)itemsList.Values[i]).ProductName;
        }
    }
}

So, what is the precedence for the cast? Is a cast an operator? What about the as keyword - is that an operator and what is its precedence?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
MDiesel
  • 2,647
  • 12
  • 14

4 Answers4

14

Its quite simple really.

When you don't wrap the cast in brackets.. you're casting the entire expression:

return (Product)itemsList.Values[i].ProductName;
//              |______________________________|

You're essentially casting a string to a Product. Whereas:

return ((Product)itemsList.Values[i]).ProductName;
//     |____________________________|

Casts just that part, allowing the . to access the properties of a Product. Hopefully the bars help show you the difference more clearly.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • That's a wonderful explanation your made there. +1 for the visual implementation. – hsim Feb 27 '14 at 21:59
  • 2
    "When you don't wrap the cast in brackets.. you're casting the entire expression" is true *for this specific example*, because the "." is a Primary operator and has the highest level of precedence while the (T)x operator is Unary and is second-highest. It is not necessarily true for all expressions! – Christopher Jun 22 '17 at 16:47
4

x.y has a higher precedence than the cast:

7.3.1 Operator precedence and associativity

The following table summarizes all operators in order of precedence from highest to lowest:

Primary x.y f(x) a[x] x++ x-- new typeof default checked unchecked delegate

Unary + - ! ~ ++x --x (T)x

Lee
  • 142,018
  • 20
  • 234
  • 287
1

It is not about precedence. Always value is converted

// Does compile. Precedence for the "." higher than the cast?
 return ((Product)itemsList.Values[i]).ProductName;

in your case value is being returned by itemsList.Values[i] which is being casted into Product. Then you are trying to access ProductName from it.

CAST is an Operator

Is/as works only on reference types

 return (itemsList.Values[i] as Product).ProductName;

READ MORE to understand difference between CAST and AS

Community
  • 1
  • 1
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
1

(Product)itemsList.Values[i].ProductName; means (Product)(itemsList.Values[i].ProductName); whereas second line you explicitly say to cast Values[i] and then do .ProductName;

LB2
  • 4,802
  • 19
  • 35