Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted May 26, 2023, 6:51 AM EDT
Sure! Here's an example of a
UserLoginclass with constructor overloading in C#:public class UserLogin { public string Username { get; private set; } public string Password { get; private set; } // Default constructor public UserLogin() { // Set default values Username = string.Empty; Password = string.Empty; } // Constructor with username and password parameters public UserLogin(string username, string password) { Username = username; Password = password; } // Constructor with username parameter only public UserLogin(string username) { Username = username; Password = string.Empty; // Set password to empty string by default } }In this example, the
UserLoginclass has three constructors:The default constructor initializes the
UsernameandPasswordproperties with empty strings.The second constructor takes
usernameandpasswordparameters and sets the corresponding properties accordingly.The third constructor takes a
usernameparameter only and sets theUsernameproperty with the provided value, while thePasswordproperty is set to an empty string by default.You can create
UserLoginobjects using different constructor variations depending on your requirements. Here are some usage examples:// Create a UserLogin object using the default constructor UserLogin user1 = new UserLogin(); // Create a UserLogin object with a username and password UserLogin user2 = new UserLogin("johnsmith", "password123"); // Create a UserLogin object with a username only UserLogin user3 = new UserLogin("janedoe");