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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:09:45+00:00 2026-05-24T08:09:45+00:00

This question has to do with best practices… please do not confuse it with

  • 0

This question has to do with best practices… please do not confuse it with premature optimization.

I noticed in .Net that deep object referencing takes quite some time to process when compared to flat or shallow objects. For example:

object1.object2.object3.object4.Property

… is not efficient since, I conclude, each object has to be de-referenced along the path. This is in contrast to C, where Property‘s memory ref would be calculated by the compiler, rather than at run time.

Now, we obviously wouldn’t take all the fields of object2+ and flatten them into object1 just for speed. The coding would be unwieldy and hard to manage. But the speed difference could be significant.

So my question is, “What is the best practice in building deep objects vs. flat(ish) objects?” And, further, is there any advantage to using structs where one is simply trying to group a number of fields together, such as:

public struct SizeData
{
    public long Written;
    public long Read;
    public int Size;
}

The two questions relate.

EDIT

To illustrate:

public class Leaf
{
    public int Property;
}

public class Depth1
{
    public Leaf Leaf;
}

public class Depth2
{
    public Depth1 Depth1;
}

public class Depth3
{
    public Depth2 Depth2;
}

private void button12_Click(object sender, EventArgs e)
{
    Depth3 depth = new Depth3();
    depth.Depth2 = new Depth2();
    depth.Depth2.Depth1 = new Depth1();
    depth.Depth2.Depth1.Leaf = new Leaf();
    Leaf leaf = new Leaf();

    var T1 = Environment.TickCount;

    for (int i = 0; i < 100000000; i++)
    {
        depth.Depth2.Depth1.Leaf.Property++;
    }

    var T2 = Environment.TickCount;

    for (int i = 0; i < 100000000; i++)
    {
        leaf.Property++;
    }

    var T3 = Environment.TickCount;

    MessageBox.Show((T2 - T1).ToString() + Environment.NewLine +
        ((T3 - T2).ToString()));
}
  • 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-24T08:09:46+00:00Added an answer on May 24, 2026 at 8:09 am

    The only concern here IMHO is not speed, but dependency. Have you ever tried to refactor an application that frequently uses deep object nesting like this? It’s a pain in the arse. Have a look at the Law of Demeter (or as I say, the Guideline of Demeter). The idea is that instead of using

    person.Address.ZipCode
    

    you instead use something like

    person.AddressZipCode
    

    to avoid a dependency on the Address object. Is it faster? Maybe, if you implement it right. But I personally don’t care because the speedup is really trivial. All I care about here is reducing dependencies.

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

Sidebar

Related Questions

I'm sure that this question has been addressed in many best practices books, but
I realize that what counts as premature optimization has a subjective component, but this
Disclaimer: the author has tried his best to ask a question that will not
Warning: This question has been heavily edited. I tried my best to guess the
If this question has already been asked I appologies, please point me in the
This is a question about best practices i guess but it applies directly to
Colleagues, Preamble. My question is more about best practices. I know one workaround. This
I have a best practices/performance question. I am creating an ASP.NET MVC 2 project,
This is a best practices question. I am making an array type * x
This is more of a best practices question. I have a few email templates,

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.