constructor that initializes an Account's name. - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Introduction

Using the Account constructor to set an Account's name when an Account object is created.

Demo Code

using System;//w  w w .  j ava 2 s  .c om
class Account
{
   public string Name { get; set; } // auto-implemented property
   // constructor sets the Name property to parameter accountName's value
   public Account(string accountName) // constructor name is class name
   {
      Name = accountName;
   }
}
class AccountTest
{
   static void Main()
   {
      // create two Account objects
      Account account1 = new Account("Mary");
      Account account2 = new Account("Edith");
      // display initial value of name for each Account
      Console.WriteLine($"account1 name is: {account1.Name}");
      Console.WriteLine($"account2 name is: {account2.Name}");
   }
}

Result


Related Tutorials