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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:25:52+00:00 2026-06-03T09:25:52+00:00

I’m currently working on a problem that involves System.Reflection.Emit code generation. I’m trying to

  • 0

I’m currently working on a problem that involves System.Reflection.Emit code generation. I’m trying to figure out what CIL to emit in places where I would use default(SomeType) in C#.

I’ve run a few basic experiments from Visual Studio 11 Beta. JustDecompile shows me the following CIL output for default(bool), default(string), and default(int?:

.locals init (
    [0] bool                                         V_0,
    [1] string                                       V_1,
    [2] valuetype [mscorlib]System.Nullable`1<int32> V_2    
)

// bool b = default(bool);
ldc.i4.0
stloc.0

// string s = default(string);
ldnull
stloc.1

// int? ni = default(int?);
ldloca.s V_2
initobj valuetype [mscorlib]System.Nullable`1<int32>

Judging from this, default(T) seems to get resolved by the compiler to the most appropriate CIL for the given types.


I went on to see what would happen in the more general case, using three generic methods:

T CreateStructDefault<T>() where T : struct { return default(T); }
T CreateClassDefault<T>()  where T : class  { return default(T); }
T CreateClassNull<T>()     where T : class  { return null;       }

All three methods produce the same CIL method body:

.locals init (
    [0] !!T V_0,
    [1] !!T V_1
)

IL_0000: nop    
IL_0001: ldloca.s V_1
IL_0003: initobj !!T
IL_0009: ldloc.1
IL_000a: stloc.0
IL_000b: br.s IL_000d
IL_000d: ldloc.0
IL_000e: ret

Question:

Can I conclude from all this that C#’s default(SomeType) corresponds most closely to CIL’s…

  • initobj for non-primitive types (except string?)
  • ldc.iX.0 / ldnull / etc. for primitive types (plus string)?

And why does CreateClassNull<T> not just translate to ldnull, but to initobj instead? After all, ldnull was emitted for string (which is also a reference type).

  • 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-06-03T09:25:53+00:00Added an answer on June 3, 2026 at 9:25 am

    Can I conclude from all this that C#’s default(SomeType) corresponds most closely to CIL’s initobj for non-primitive types and ldc.i4.0, ldnull, etc. for primitive types?

    That’s a reasonable summary but a better way to think about it is: if the C# compiler would classify default(T) as a compile time constant then the value of the constant is emitted. That is zero for numeric types, false for bool, and null for any reference type. If it would not be classified as a constant then we must (1) emit a temporary variable, (2) obtain the address of the temporary, (3) initobj that temporary variable via its address and (4) ensure that the temporary’s value is on the stack when it is needed.

    why does CreateClassNull<T> not just translate to ldnull, but to initobj instead?

    Well, let’s do it your way and see what happens:

    ... etc
    .class private auto ansi beforefieldinit P
           extends [mscorlib]System.Object
    {
      .method private hidebysig static !!T  M<class T>() cil managed
      {
        .maxstack  1
        ldnull
        ret
      } 
      ... etc
    

    …

    D:\>peverify foo.exe
    
    Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.17379
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    [IL]: Error: 
    [d:\foo.exe : P::M[T]]
    [offset 0x00000001]
    [found Nullobjref 'NullReference']     
    [expected (unboxed) 'T'] 
    Unexpected type on the stack.
    1 Error(s) Verifying d:\foo.exe
    

    That would probably be why we don’t do that.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am currently running into a problem where an element is coming back from
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed 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.