Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Lazy-loading a Dictionary's Value Collection ?

Lazy-loading a Dictionary's Value Collection ?

Scheduled Pinned Locked Moved C#
questioncsharpdelphisalestutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BillWoodruff
    wrote on last edited by
    #1

    Code example (uses .NET 4.7):

    public enum Divisions
    {
    Undefined,
    Admin,
    Finance,
    Sales,
    Research,
    IT,
    Temp
    }

    // 'PersonNode code not shown here: it's just wrapper around a POCO

    public Dictionary>> DivisionToStaff
    { get; set; } = new Dictionary>>();

    void Initialize()
    {
    foreach (Divisions div in Enum.GetValues(typeof(Divisions)))
    {
    DivisionToStaff.Add(div, new Lazy>());
    }
    }

    // sample usage : note access is via Lazy 'Value property DivisionToStaff[node.Division].Value.Add(personnode);

    Here's my question: I wonder why I have to use 'new in the initialization code for the lazy-loaded lists in the Dictionary Values: I thought I could just set the Values to 'null ? Out of curiosity is there a zen-like way to make the entire Dictionary lazy-loaded ? thanks, Bill

    «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups