Create read only fields - CSharp Custom Type

CSharp examples for Custom Type:read only

Description

Create read only fields

Demo Code

using static System.Console;
using System;/*from   w ww . ja  v a  2 s.c  o  m*/
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var p3 = new Person();
      WriteLine($"{p3.Name} was instantiated at  {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM  yyyy}");
      var p4 = new Person("Aziz");
      WriteLine($"{p4.Name} was instantiated at {p4.Instantiated:hh:mm:ss} on {p4.Instantiated:dddd, d MMMM yyyy}");
   }
}
public class Person : object
{
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
   public readonly DateTime Instantiated;
   public const string Species = "Programmer";
   public readonly string HomePlanet = "Earth";
   public Person()
   {
      Name = "Unknown";
      Instantiated = DateTime.Now;
   }
   public Person(string initialName)
   {
      Name = initialName;
      Instantiated = DateTime.Now;
   }
}

Result


Related Tutorials