Initialize read only field in constructor - CSharp Custom Type

CSharp examples for Custom Type:read only

Description

Initialize read only field in constructor

Demo Code

using System;/*from w w  w .  ja v a 2s .co m*/
class Account
{
   public readonly string AccountNumber;
   public Account (string permanentAccountNumber)
   {
      AccountNumber = permanentAccountNumber;
   }
}
class SimpleBank
{
   public static void Main()
   {
      Account yourAccount = new Account("8487-9873-9938");
      Console.WriteLine("Your account number: "
      + yourAccount.AccountNumber);
   }
}

Result


Related Tutorials