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 7169619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:02:10+00:00 2026-05-28T15:02:10+00:00

accidentally at work I wrote the following line of code: string x = (object)

  • 0

accidentally at work I wrote the following line of code:

string x = (object) null; 
// It was var x = (object)null and I changed from var to string instead of 
// object x = null;

This gave me a compilation error similar to this: Can't cast source type object to target type string

Why? Isn’t null just a bunch of zeros pointing to “nowhere” memory address, no matter what the type is?

  • 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-28T15:02:11+00:00Added an answer on May 28, 2026 at 3:02 pm

    The question here is basically “why does the compiler not take into account the fact that it knows that the assigned value is a constant reference known to be null?”

    The answer is: why should it? What’s the compelling benefit of taking that information into account? You deliberately said “I want this expression to be treated as though it were of type object”, and you can’t assign a value of type object to a variable of type string. What’s the benefit of allowing that in this case?

    The code seems to me to be highly likely to be a bug; surely the compiler should be telling you about it rather than allowing it.

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

Sidebar

Related Questions

This morning I accidentally saw the following snippet code, I was fairly surprised because
I've accidentally written this line of code: scala> val f = Int f: Int.type
More than once I've lost work by accidentally killing a temporary buffer in Emacs.
I accidentally committed too many files to an SVN repository and changed some things
I accidentally pushed a commit to GitHub. Is it possible to remove this commit?
Accidentally I found this post about a new feature in ASP.NET 4.0: Expressions enclosed
I'm using Shared Linux hosting and some PHP I wrote just won't work with
I accidentally came across $(undefined) in a statement below, where params is an object:
I usually work on my Master branch, and push to Production. I accidentally worked
Ok, so I work on a project from home and work using 2 computers

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.