[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-03-03
1
Medium Priority
?
207 Views
Last Modified: 2014-03-11
Can someone tell me what this line of code mean.... MyObject obj1 = obj as MyObject;

class MyObject : IComparable
{
public int ID{ get; set; }
public int CompareTo(object obj)
{
MyObject obj1 = obj as MyObject;
return this.ID.CompareTo(obj1.ID);
}
}

Open in new window

0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 39902123
It's a cast. Ordinarily, you use parentheses and a type name to perform a cast:

e.g.

MyObject obj1 = (MyObject)obj;

Open in new window


This has the consequence that if obj is not actually of type MyObject, or descended from it, then you will encounter an exception. The as operator also performs casts, but it has the added benefit that if the cast will not succeed--that is, if the thing being cast isn't of the type being cast to (or descended from it)--then as will return null. It's a good way to confirm a cast will succeed before working with the casted reference.

A word of warning:  Since as can return null you should be checking for null any time you use as:

e.g.

MyObject obj1 = obj as MyObject;

if (obj1 != null)
{
    // Cast succeeded
}

if (obj1 == null)
{
    // Cast failed
}

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 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