Initialize object with code block - CSharp Custom Type

CSharp examples for Custom Type:Object Initializer

Description

Initialize object with code block

Demo Code

using static System.Console;
using System;//  w  w  w  . java  2 s  .co  m
class Program
{
   static void Main(string[] args)
   {
      var p2 = new Person
      {
         Name = "back",
         DateOfBirth = new DateTime(2020, 3, 17)
      };
      WriteLine($"{p2.Name} was born on {p2.DateOfBirth:d MMM yy}");
   }
}
public class Person : object
{
   // fields
   public string Name;
   public DateTime DateOfBirth;
}

Result


Related Tutorials