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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:31:04+00:00 2026-05-23T17:31:04+00:00

In the following code, boxing occurs (in Generic<Type>.Print): using System; namespace Test { static

  • 0

In the following code, boxing occurs (in Generic<Type>.Print):

using System;

namespace Test
{
    static class Program
    {
        static void Main()
        {
            Generic<string> generic = new Generic<string>("test");
            generic.Print();
        }
    }

    class Generic<Type>
    {
        Type value;

        public Generic(Type value)
        {
            this.value = value;
        }

        public void Print()
        {
            Console.WriteLine(value);
        }
    }
}

ILSpy output:

.method public hidebysig 
    instance void Print () cil managed 
{
    // Method begins at RVA 0x207d
    // Code size 17 (0x11)
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: ldfld !0 class Test.Generic`1<!Type>::'value'
    IL_0006: box !Type
    IL_000b: call void [mscorlib]System.Console::WriteLine(object)
    IL_0010: ret
} // end of method Generic`1::Print

It’s boxing and calling Console.WriteLine(object). I assumed that it would simply call Console.WriteLine(string). What is going on here?

  • 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-23T17:31:04+00:00Added an answer on May 23, 2026 at 5:31 pm

    Its chosen the object overload for Console.WriteLine because this is the most appropriate overload for that call.

    Remember that overload resolution is done at compile time – the compiler must choose a suitable overload based on the provided type information, and in this situation the only suitable one is the object overload.

    To understand this it may help to ignore your Main method and consider the case where the Generic class is in a different assembly. The compiler needs to choose an overload and knows only that Type can either be cast or boxed into an object. Just because there is in fact code elsewhere in the assembly that uses this class with a string type parameter doesn’t affect the way that Generic is compiled.

    Alternatively consider what would happen if Console.WriteLine didn’t have an overload accepting an object – in this case the method simply would not compile (as there are no restrictions on Type which would make another overload suitable).

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

Sidebar

Related Questions

I have some questions about the following code: using System; namespace ConsoleApplication2 { public
// following code works fine n open notepad... class demo { public static void
Following code fails with a error message : t.cpp: In function `void test()': t.cpp:35:
The following code: template <typename S, typename T> struct foo { void bar(); };
Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
Consider the following code: class Foo { // boring parts omitted private TcpClient socket;
I need to use java-legacy code with the following method: public void doit(Map <String,
I am having issues with the following code in Scala import org.junit.Test import org.junit.Assert._
Following code shows that the parameter, passed by reference, is copied when using boost::bind.
The following code doesn't work in python x = 11 print(x += 5) while

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.