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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:48:44+00:00 2026-05-10T20:48:44+00:00

Say you have a class declaration, e.g.: class MyClass { int myInt=7; int myOtherInt;

  • 0

Say you have a class declaration, e.g.:

 class MyClass {   int myInt=7;   int myOtherInt; } 

Now, is there a way in generic code, using reflection (or any other means, for that matter), that I can deduce that myInt has a default value assigned, whereas myOtherInt does not? Note the difference between being initialised with an explicit default value, and being left to it’s implicit default value (myOtherInt will be initialised to 0, by default).

From my own research it looks like there is no way to do this – but I thought I’d ask here before giving up.

[Edit]

Even with nullable and reference types I want to distingush between those that have been left as null, and those that have been explicitly initialised to null. This is so that I can say that fields with an initialiser are ‘optional’ and other fields are ‘mandatory’. At the moment I’m having to do this using attributes – which niggles me with their redundancy of information in this case.

  • 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. 2026-05-10T20:48:45+00:00Added an answer on May 10, 2026 at 8:48 pm

    I compiled your code and load it up in ILDASM and got this

    .method public hidebysig specialname rtspecialname          instance void  .ctor() cil managed {     // Code size       15 (0xf)     .maxstack  8     IL_0000:  ldarg.0     IL_0001:  ldc.i4.7     IL_0002:  stfld      int32 dummyCSharp.MyClass::myInt     IL_0007:  ldarg.0     IL_0008:  call       instance void [mscorlib]System.Object::.ctor()     IL_000d:  nop     IL_000e:  ret } // end of method MyClass::.ctor 

    Note the ldc.i4.7 and stfld int32 dummyCSharp.MyClass::myInt seems to be instructions to set the default values for the myInt field.

    So such assignment is actually compiled as an additional assignment statement in a constructor.

    To detect such assignment, then you will need reflection to reflect on the IL of MyClass’s constructor method and look for stfld (set fields?) commands.


    EDIT: If I add some assignment into the constructor explicitly:

    class MyClass {     public int myInt = 7;     public int myOtherInt;      public MyClass()     {         myOtherInt = 8;     } } 

    When I load it up in ILDASM, I got this:

    .method public hidebysig specialname rtspecialname                  instance void  .ctor() cil managed {     // Code size       24 (0x18)     .maxstack  8     IL_0000:  ldarg.0     IL_0001:  ldc.i4.7     IL_0002:  stfld      int32 dummyCSharp.MyClass::myInt     IL_0007:  ldarg.0     IL_0008:  call       instance void [mscorlib]System.Object::.ctor()     IL_000d:  nop     IL_000e:  nop     IL_000f:  ldarg.0     IL_0010:  ldc.i4.8     IL_0011:  stfld      int32 dummyCSharp.MyClass::myOtherInt     IL_0016:  nop     IL_0017:  ret } // end of method MyClass::.ctor 

    Note that the extra assigment on myOtherInt that I added was addded after a call the Object class’s constructor.

    IL_0008:  call       instance void [mscorlib]System.Object::.ctor() 

    So there you have it,

    Any assignment done before the call to Object class’s constructor in IL is a default value assignment.

    Anything following it is a statement inside the class’s actual constructor code.

    More extensive test should be done though.

    p.s. that was fun 🙂

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

Sidebar

Related Questions

Let's say you have a class with a Uri property. Is there any way
Say I have class A with class A { final String foo() { //
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
Say I have a class named Frog, it looks like: public class Frog {
Say I have a class called PermissionManager which should only exist once for my
Say I have a class with a private dispatch table. $this->dispatch = array( 1
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a class: class Foo { public string Bar { get
Let's say I have a class that has a member called data which is

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.