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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:48:05+00:00 2026-06-02T16:48:05+00:00

I am not sure if this is a unity bug but can anyone help

  • 0

I am not sure if this is a unity bug but can anyone help me explain why the below program in the third case prints 150?

void Main()
{
// Test 1
Derived d = new Derived();
d.Height.Dump(); //Prints 10

// Test 2
IUnityContainer unityContainer = new UnityContainer();
unityContainer.Resolve(typeof(Derived)).Dump(); // Prints 10

// Test 3 
unityContainer.RegisterType<IPrintHeightService<Derived>, PrintHeightService<Derived>>();
var output = unityContainer.Resolve<IPrintHeightService<Derived>>();
output.Show(); //Prints 150
}


public interface IHasHeight
{
    Int32 Height {get; set;}
}

// Define other methods and classes here
public class Default : IHasHeight
{
    public Int32 Height {get; set;}

    public Default()
    {
        Height = 150;
    }
}

public class Derived : Default
{
    public new Int32 Height {get { return 10;}}
}


public interface IPrintHeightService<T> where T:IHasHeight, new() 
{
    void Show();
}

public class PrintHeightService<T> : IPrintHeightService<T> where T:IHasHeight, new()
{
    private IHasHeight objectWithHeight;
    public PrintHeightService(IUnityContainer unityContainer)
    {
        objectWithHeight = (T) unityContainer.Resolve(typeof(T));
    }

    public void Show()
    {
        Console.WriteLine(objectWithHeight.Height); // Prints 150
    }
}

I suspect this is because of the intializers are run from derived to base as explained (http://blogs.msdn.com/b/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx) but why it is obvious with unity and not otherwise?

Thanks much.

  • 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-02T16:48:07+00:00Added an answer on June 2, 2026 at 4:48 pm

    It’s because you declared Height as new. By declaring it as new, Derived doesn’t override Height, it just hides it. That has nothing to do with the initializers.

    Which means that the new implementation doesn’t have anything to do with the virtual Property declared by the Interface, it just happens to have the same name, thus “hiding” it. By calling Height over IHasHeight (in the Show() method of PrintHeightService you access a field with that type), the property declared by the interface is called, which is implemented in Default. You will only get Derived.Height, if you call it via a reference of the type Derived.

    You can see for yourself: Change the type of the variable d in example 1 to Default or IHasHeight and you’ll see that you will get 150, too.

    If you truly want to override the implementation of Height in Derived

    • Declare Height as virtual in Default and then
    • Declare Height as override in Derived
    • And remove the new modifier.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure if I have this right ... but can anyone point
I'm not sure this is possible, but in ruby, you can dynamically call a
So I'm using Unity to do this, but I'm not sure that matters. I
Not sure this is possible, but looking to write a script that would return
I'm not sure this is possible, but is there a syntax to be used
I'm not sure this question is appropriate here but I hope I could get
I'm sure this is a fairly trivial problem, but I'm not sure what to
Not sure quite how to describe this but this is what i want to
I am not sure if this is possible but I am trying to come
I'm still not sure this is the correct way to go about this, maybe

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.