Object Initial Value Setting and set initial value setting, object set
Directly run the Code:
Class Program {static void Main (string [] args) {StudentName student1 = new StudentName ("Craig", "Playstead "); // to set the object's initial value, you must first access the default instance constructor // use the object's initial value to initialize the object StudentName student2 = new StudentName {FirstName = "Craig ", lastName = "Playstead"}; StudentName student3 = new StudentName {ID = 183}; StudentName student4 = new StudentName {FirstName = "Craig", LastName = "Playstead ", ID = 116}; Console. writeLine (student1.ToString (); Console. writeLine (student2.ToString (); Console. writeLine (student3.ToString (); Console. writeLine (student4.ToString (); Console. writeLine (); // use the set initialization item to initialize the Dictionary <int, StudentName> students = new Dictionary <int, StudentName> () {111, new StudentName {FirstName = "coming in", LastName = "Karnik", ID = 211 }}, {112, new StudentName {FirstName = "Dina", LastName = "Salimzianova ", ID = 317 },{ 113, new StudentName {FirstName = "Andy", LastName = "Ruth", ID = 198 }}}; // dictionary example // foreach (KeyValuePair <int, StudentName> s in students) // {// Console. writeLine (s. value. firstName + "" + s. value. lastName + "" + s. value. ID); //} Dictionary <int, StudentName>. keyCollection keys = students. keys; foreach (int s in keys) {Console. writeLine (students [s]. toString ();} Console. readKey () ;}} public class StudentName {// if this is the default constructor that is private, the object's initial value is set to public StudentName () {FirstName = "Lily ";} public StudentName (string first, string last) {FirstName = first; LastName = last;} public string FirstName {get; set;} public string LastName {get; set ;} public int ID {get; set;} public override string ToString () {return FirstName + "" + LastName + "" + ID ;}}