Object Initial Value Setting and set initial value setting, object set

Source: Internet
Author: User

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 ;}}

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.