C
C#16mo 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
KaiOP16mo ago
basically just refactor it so that I don't need those methods if possible?
ero
ero16mo ago
they're not required in the first place you can just Tags.Add, Tags.Remove as normal in your code
Kai
KaiOP16mo ago
oh yeah true
ero
ero16mo 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
KaiOP16mo ago
ah thanks!
ero
ero16mo ago
that's bogus if they want it private, let them keep it private
Kai
KaiOP16mo 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
ero16mo ago
that's just wrong
Joreyk ( IXLLEGACYIXL )
ok ok
ero
ero16mo ago
bah no you're right my b
Joreyk ( IXLLEGACYIXL )
ok ok..
ero
ero16mo ago
i misread
Kai
KaiOP16mo 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
ero16mo ago
what do you mean? are you running into any issues?
Kai
KaiOP16mo ago
no but i dont like the feeling of how its written welp i guess it isnt a big deal
ero
ero16mo ago
how did you write it before?
Accord
Accord16mo 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.
Want results from more Discord servers?
Add your server