C
C#2y ago
Kai

❔ Is there a way to make this neater?

public void AddTags(string tag)
{
Tags?.Add(tag);
Tags = Tags;
}

public void RemoveTag(string tag)
{
Tags?.Remove(tag);
Tags = Tags;
}

private HashSet<String>? Tags { get; set; }
public void AddTags(string tag)
{
Tags?.Add(tag);
Tags = Tags;
}

public void RemoveTag(string tag)
{
Tags?.Remove(tag);
Tags = Tags;
}

private HashSet<String>? Tags { get; set; }
I feel like I can do it with {get; set;} but idk how
20 Replies
Kai
KaiOP2y ago
basically just refactor it so that I don't need those methods if possible?
ero
ero2y ago
they're not required in the first place you can just Tags.Add, Tags.Remove as normal in your code
Kai
KaiOP2y ago
oh yeah true
ero
ero2y ago
Tags = Tags does nothing you could do private HashSet<string> Tags { get; } = new(); to avoid the null checks as well
Joreyk ( IXLLEGACYIXL )
public HashSet<String> Tags { get; init; } = new() for more "flexibility"
Kai
KaiOP2y ago
ah thanks!
ero
ero2y ago
that's bogus if they want it private, let them keep it private
Kai
KaiOP2y ago
oh i had it private because the functions were public
Joreyk ( IXLLEGACYIXL )
the functions are public so he needs public access for the add and remove so its not bogus whatever that is
ero
ero2y ago
that's just wrong
ero
ero2y ago
bah no you're right my b
Joreyk ( IXLLEGACYIXL )
ok ok..
ero
ero2y ago
i misread
Kai
KaiOP2y ago
wait then how do i make it so that when initalizing my object that it can be added to using .Add and .Remove?
EmailMessage testEmail = new()
{
Tags = new HashSet<string> {"Important"},
Sender = new HashSet<string> {"[email protected]"},
Recipient = new HashSet<string> {"[email protected]"},
EmailBody = "Hello, World!",
Folder = new[] {FolderType.Inbox},
Subject = "Hello, World Email",
};
EmailMessage testEmail = new()
{
Tags = new HashSet<string> {"Important"},
Sender = new HashSet<string> {"[email protected]"},
Recipient = new HashSet<string> {"[email protected]"},
EmailBody = "Hello, World!",
Folder = new[] {FolderType.Inbox},
Subject = "Hello, World Email",
};
ero
ero2y ago
what do you mean? are you running into any issues?
Kai
KaiOP2y ago
no but i dont like the feeling of how its written welp i guess it isnt a big deal
ero
ero2y ago
how did you write it before?
Accord
Accord17mo ago
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.

Did you find this page helpful?