C
C#2y ago
rallez

✅ Give object default values (destroy?)

Hi I have a program which after sign in assignes object user some values. While sign out i need to delete/destroy or set as default those vaules.
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public List<Character> Characters { get; set; }

public User(string username, int userid)
{
Username = username;
UserId = userid;
Characters = new List<Character>();
}

~User()
{
Username = "";
UserId = default;
Characters = new List<Character>();;
}
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public List<Character> Characters { get; set; }

public User(string username, int userid)
{
Username = username;
UserId = userid;
Characters = new List<Character>();
}

~User()
{
Username = "";
UserId = default;
Characters = new List<Character>();;
}
}
I've heard about deconstructor but i dont know how to use them to be honest. Please help me
10 Replies
Anton
Anton2y ago
forget about the deconstructor either set the whole user to null, which would make more sense, or make a method that resets them you have to do that manually if it were a struct tho, you could've done this = default
rallez
rallez2y ago
okay but earlier in my code im creating new User user it won't give me an error?
Anton
Anton2y ago
idk I don't have your code
rallez
rallez2y ago
a ihave a login function that creates user object now i want to create a function that log out no user can sign in, log out and the again sign in for example
phaseshift
phaseshift2y ago
that is business logic, nothing to do with destroying or destructing a user instance
Thinker
Thinker2y ago
What this would do is when the instance itself is garbage collected, the destructor is run, which would make it completely useless.
rallez
rallez2y ago
so can I do that and it will be completly fine?
User user = new User(providedUsername, Data.TempUserId);
// HERE SOME CODE
User user = new User(providedUsername, Data.TempUserId);
User user = new User(providedUsername, Data.TempUserId);
// HERE SOME CODE
User user = new User(providedUsername, Data.TempUserId);
and will it overwrite it right?
phaseshift
phaseshift2y ago
well, no. 'user' is the same variable name. But apart from that, you have a valid duplicate its not overwriting anything
User user = new User(providedUsername, Data.TempUserId);
// HERE SOME CODE
user = new User(providedUsername, Data.TempUserId); // re-assign the 'user' variable.
User user = new User(providedUsername, Data.TempUserId);
// HERE SOME CODE
user = new User(providedUsername, Data.TempUserId); // re-assign the 'user' variable.
The first User instance will get GC'd at some point
Thinker
Thinker2y ago
"Garbage collected" or "GC'd" is when an object has no references throughout the entire program. It's inaccessible, gone, discarded. At that point, the destructor might be run, before the memory is freed entirely, but you can't really do anything of value at that point. anyway, as already stated, destructors are not the right tool here.
Accord
Accord2y 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
More Posts