C
C#

help

❔ Is it worth to create lazy dictionary in this case?

Iivefifthsence2/3/2023
I have following class: so as you see Add function add to the dictionary class TreeNode, it looks like a tree I made a lazy dictionary and my question is: is it worth it? Will it be better with simple dictionary?
public class TreeNode
{
public readonly Lazy<Dictionary<string, TreeNode>> _treeChildren = new();

public bool Add(
string[] subTopic,
MqttDelivery delivery)
=> Add(subTopic, Level + 1, delivery);

private bool Add(
string[] topic,
int level,
MqttDelivery delivery)
{
if (level >= topic.Length)
{
Topic = delivery.Topic;
Header = delivery.Header?.ToJsonString( true );
Payload = delivery.Payload?.ToJsonString( true );
Delivery = delivery;
AddToHistory( delivery );

return false;
}

var part = topic[ level ];
if (!_treeChildren.Value.ContainsKey(part))
{
TopicsCount++;
_treeChildren.Value.Add( part, new TreeNode( part, level, this));
}

MessagesCount++;
_treeChildren.Value[ part ].Add( topic, level + 1, delivery ); return true;
}
}
public class TreeNode
{
public readonly Lazy<Dictionary<string, TreeNode>> _treeChildren = new();

public bool Add(
string[] subTopic,
MqttDelivery delivery)
=> Add(subTopic, Level + 1, delivery);

private bool Add(
string[] topic,
int level,
MqttDelivery delivery)
{
if (level >= topic.Length)
{
Topic = delivery.Topic;
Header = delivery.Header?.ToJsonString( true );
Payload = delivery.Payload?.ToJsonString( true );
Delivery = delivery;
AddToHistory( delivery );

return false;
}

var part = topic[ level ];
if (!_treeChildren.Value.ContainsKey(part))
{
TopicsCount++;
_treeChildren.Value.Add( part, new TreeNode( part, level, this));
}

MessagesCount++;
_treeChildren.Value[ part ].Add( topic, level + 1, delivery ); return true;
}
}
Ddemidev_mb2/3/2023
It's not a lazy dictionary, it's just lazily created dictionary. No it's not worth it Just use this
_treeChildren ??= new();
_treeChildren ??= new();
private readonly Dictionary<string, TreeNode> _treeChildren; // null
private readonly Dictionary<string, TreeNode> _treeChildren; // null
If it's public use property
AAccord2/4/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Going to use a listbox with a switch for something in the morning, need help with format for bothI would like to set two variables with a listbox using a switch and one variable with a second listb✅ What's your view on tool like Sonarqube?Should I learn from the code smells it captures?✅ Autodesk Maya HelpAnyone in here know Autodesk Maya really well? My mesh tool isn't letting my polygon primitives mesh❔ circular primesso this program is meant to find how many circular primes there are below 1 million (should be 55), ❔ WPF Cannot find child when it's thereHi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Pr❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in ❔ Doing post method with entity frameworki'm making a simple API to practice with entity framework but when i try to make a post to a table w❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my windowException/NULL/Error handling advice.DeserializeAsync clearly returns a nullable generic object. From the picture we can clearly see that❔ ASP.NET Core web app vs ASP.net web application(.NET Framework)Hi, Ive been working with c# for a while now and would like to make a webapp but I dont know where t✅ IGNORE PEOPLEmy place for testing commands