Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6240787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:38:06+00:00 2026-05-24T11:38:06+00:00

I hope this is appropriate for this site because I already figured out the

  • 0

I hope this is appropriate for this site because I already figured out the answer, so it’s more a quiz than a question.

This C# code works with no problem:

WidgetRef = widget as IWidget;
WidgetRef.Init();

However if I try changing it to:

WidgetRef = (IWidget)widget;
WidgetRef.Init();

in some situations I get a “cannot cast to IWidget” exception.

At first I was dumfounded how that could be possible, because if it can’t cast in the 2nd example, it should throw a null exception in the 1st example. But I discovered it ain’t necessarily so 🙂

How is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:38:08+00:00Added an answer on May 24, 2026 at 11:38 am

    OK, here’s the answer….

    The incorrect assumption that I was making as that WidgetRef was a variable or field. It was actually a property, defined like this:

        private IWidget _widgetRef;
        private IWidget WidgetRef
        {
            get { return _widgetRef ?? new NullWidget(); }
            set { _widgetRef = value; }
        }
    

    where NullWidget is a class implementing IWidget in a minimal way. So even though null was being assigned in to WidgetRef, that wasn’t what was coming out of it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure this question is appropriate here but I hope I could get
This is more of knowledge sharing rather than asking a question. Thought this little
I hope this is the appropriate title for this question. Right now I have
I hope this question is appropriate for stackoverflow... What is the difference between storing
I hope this question is considered appropriate for stackoverflow. If not, I'll remove the
I hope this is an appropriate question: I'm using the Netflix API and I'm
Hope this question is not stupid since I am an amateur web designer. I
hope this will be a quick easy question, I've just tried my app on
I hope this question isn't too general. Well, the situation is that I am
I hope this question isn't too dumb. We have many websites internally that need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.