Append () Usage
Class preson
{
Private string firstname;
Private string middletname;
Private string lastname; // defines three private variables: firstname middlename lastname
Public preson (string FN, string Mn, string ln) // Constructor
{
Firstname = FN;
Middlename = Mn;
Lastname = ln;
}
Public void displayfull name () // define a method name
{
Stringbuilder fullname = new stringbuilder (); // instantiate fullname
Fullname. append (firstname); // fullname = fullname + firstname full is empty. Only firstname is displayed.
Fullname. append (""); // fullname = fullname + "space" fullname already contains the value of firstname
If (middlename [0]! = "") // If the character in the first array of the intermediate name is not empty, run downward. Otherwise, the IF statement is displayed.
{
Fullname. append ("."); // if the condition is met, continue fullname = fullname + "."
Fullname. append (lastname); // value of fullname = fullname + lastname
Console. writeline (fullname); // output fullname
}
}
Public static void main ()
{
Person me = new person ("Bradley", "Lee", "Jones"); // instantiate
Me. displayfullname (); // call this function
}
Output result: Bradley L. Jones
Explanation: fullname-> Bradley assigns a value first.
Fullname-> Bradley _ add a space ("_" indicates that the space is not an underline)
Fullname-> bradley_l
Fullname-> bradley_l.
Fullname-> bradley_l.jones
Fullname = Bradley L. Jones