Using an object initializer initializes

Recording using an object initializer to initialize the object.

the using the System;
 the using the System.Collections.Generic; 

namespace ConsoleApp2 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // use constructor initializes the object 
            StudentName student1 = new new StudentName ( " Craig " , " Playstead " ); 

            // declaratively initialize type of object, calling the default constructor, a default constructor must be public 
            StudentName student3 = new new StudentName 
            { 
                ID = 183 is
            }; 

            // declaratively initialize the type of object, calling the default constructor, a default constructor must be public 
            StudentName student4 = new new StudentName 
            { 
                FirstName = " Craig " , 
                the LastName = " Playstead " , 
                ID = 1 16 
            }; 

            // object initializer setting item can be used to set the index in the object 
            var Team = new new the BaseballTeam 
            { 
                [ . 4 ] = " of Jose Altuve " , 
                [" The RF " ] = " Mookie Betts " , 
                [ " CF2 " ] = " Mike Trout " 
            }; 

            Console.WriteLine (Team [ " 2B " ]); 
        }      
    } 
    public  class StudentName 
    { 
        // If the private, can not declaratively type of object initialization 
        public StudentName () {} 
       
        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() => FirstName + " " + ID; } public class BaseballTeam { private string[] players = new string[9]; private readonly List<string> positionAbbreviations = new List<string> { "P", "C", "1B", "2B", "3B", "SS", "LF", "CF", "RF" }; public string this[int position] { // Baseball positions are 1 - 9. get { return players[position - 1]; } set { players[position - 1] = value; } } public string this[string position] { get { return players[positionAbbreviations.IndexOf(position)]; } set { players[positionAbbreviations.IndexOf(position)] = value; } } } }

 

Guess you like

Origin www.cnblogs.com/bibi-feiniaoyuan/p/12609296.html