• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 878
  • Last Modified:

C# Object Oriented Question

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?
1 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.
guyrisoAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now