C# Object Oriented Question

Posted on 2012-08-31
Last Modified: 2012-08-31
I was working with the System.ServiceModel.Syndication namespace and I was using an instance of the SyndicationItem class and I was trying to read a value from item.content property.  Item.content is of the type System.ServiceModel.Syndication.TextSyndicationContent and it has a property called text.  I was able to see this value in my  debugger and when I put a watch on it, it gave me this:
((System.ServiceModel.Syndication.TextSyndicationContent)(item.Content)).Text.  I have a good understanding of OOP, but I have never run into an instance where I needed to use this type of syntax.  I would like to someone to explain to me why I needed to read this value using this syntax.  Can someone explain to me why this works that way?  What does this statement: ((System.ServiceModel.Syndication.TextSyndicationContent)(item.Content)).Text mean?
Question by:guyriso
    LVL 37

    Accepted Solution

    It just looks like a cast to me. Just like you can do (unsigned int)x in C to use x as an unsigned int.
    The parentheses around item.Content are just because the system likes to add parentheses where it can to ensure nothing messes up if there is a more complicated expression used.

    Then after the cast, the .Text property is accessed.

    What I not positive on is why the system bothers casting if it already is that type. Perhaps it is just a safeguard like the extra parentheses or perhaps your variable is a derived type and it's casting back to the base type.

    Author Comment

    I think I understand better now.  It I were to write item.content.text, I would say that text is a property of content.  Content does not have a property called text, the underlying datatype has the property of text therefore it had to be casted first to access that property.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    One of the most frequent problems a "newbie" developer may encounter is having to deal with different data formats. One for all: THE DATE We, as humans, need to "see" a date and then interpret it (much of the times this is an automatic operation)…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now