UserProfileusernamepasswordUsername that returns the usernamePassword that sets the passwordDisplayInfo that prints the username and a message about the passwordJump into concepts and practice - no test required
UserProfileusernamepasswordUsername that returns the usernamePassword that sets the passwordDisplayInfo that prints the username and a message about the passwordUserProfile with two private string fields: username and password. Initialize username to "Alice" and password to "" (empty string).Use private string username = "Alice"; and private string password = ""; inside the class.
UserProfile class, add a read-only property called Username that returns the username field, and a write-only property called Password that sets the password field.Use public string Username { get { return username; } } and public string Password { set { password = value; } }.
DisplayInfo inside UserProfile that prints the username and the message "Password is set" if the password is not empty, otherwise "Password is not set".Use System.Console.WriteLine to print the username and check if password is empty or not.
Main method, create an instance of UserProfile called user. Set the Password property to "secret123". Then call DisplayInfo() on user to print the username and password status.Create UserProfile user = new UserProfile();, set user.Password = "secret123";, then call user.DisplayInfo();.
Which statement correctly describes a read-only property in C#?
get accessor allows reading but not writing.set accessor, so they cannot be assigned a value.Which of the following is the correct syntax for a write-only property named Password in C#?
public string Password { ? }set accessor to allow writing but no reading.set accessor with assignment, which is correct for write-only.What will be the output of the following C# code?
class User {
private string _name = "Alice";
public string Name { get { return _name; } }
}
var user = new User();
Console.WriteLine(user.Name);Name property is read-only with a get accessor returning "Alice".user.Name returns "Alice", so it prints "Alice".Identify the error in this C# code snippet:
class Account {
private string _pin;
public string Pin {
get { return _pin; }
set { _pin = value; }
}
}
var acc = new Account();
acc.Pin = "1234";
Console.WriteLine(acc.Pin);The goal is to make Pin write-only.
get and set accessors, allowing reading and writing.set accessor and no get accessor.You want to create a class Secret that stores a string value. The value should be settable but never readable from outside the class. Inside the class, you want to log the value whenever it is set. Which property implementation achieves this?
set accessor to be write-only and log the value when set.set accessor that logs and assigns the value, matching requirements.