Define const fields in class - CSharp Custom Type

CSharp examples for Custom Type:Constant

Description

Define const fields in class

Demo Code

using static System.Console;
using System;// w  ww. java  2s  .co m
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var p2 = new Person
      {
         Name = "Back",
         DateOfBirth = new DateTime(1998, 3, 17)
      };
      WriteLine($"{p2.Name} is a {Person.Species}");
      WriteLine($"{p2.Name} was born on {p2.HomePlanet}");
   }
}
public class Person : object
{
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
   public const string Species = "Programmer";
   public readonly string HomePlanet = "Earth";
}

Result


Related Tutorials