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

  • SEARCH
  • Home
  • 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 1059413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:10:03+00:00 2026-05-16T18:10:03+00:00

(related to PropertyInfo SetValue and nulls ) If I have public class Thing {

  • 0

(related to PropertyInfo SetValue and nulls)

If I have public class Thing { public int X; }, a Thing o, and a FieldInfo fi that points to the X field, why is it legal to call fi.SetValue(o, null)? The runtime sets the field X to zero, i.e. default(int) instead of complaining that a ValueType cannot be set to null.

Does anyone know the design choice behind this behavior, which at least from C# violates my principle of least astonishment?

  • 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-16T18:10:03+00:00Added an answer on May 16, 2026 at 6:10 pm

    The text contained in the Exceptions box for ArgumentException suggests that the value passed in is subject to conversion, which would explain why it succeeds.

    The value parameter cannot be converted and stored in the field.

    I do agree that it seems slightly strange, particularly as I generally expect the reflection APIs to be one of the more rigid and less forgiving.

    You might try contacting Eric Lippert , whilst this is a BCL/CLR question rather than C#, there’s a chance he’ll know the answer or know someone who does. Either that or be able to give a very good guess.

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

Sidebar

Related Questions

Related: How can I use polymorphism in XML Serialization? I have a class I
As the topic suggests I have some problems with PropertyInfo.SetValue. To get to the
Related to my previous question: Call repaint from another class in Java? I'm new
Related to my previous post but its not a duplicate of that.Now I have
Related to an earlier question I asked, I've seen that there's both LoadComponent() and
Related to this question , is the idea of a default servlet that serves
Related to my previous question , I have a string on the following format:
Related to my other question , I have now modified the sparse matrix solver
Related to a lot of questions and answers on SO, I've learned that it's
Related to Access restriction on class due to restriction on required library rt.jar? jaxb-impl

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.